feviskus

feviskus

Hello there,

I`m currently reading Programming Phoenix and struggle to understand some parts of the code.
2 Examples:

[web/models/user.ex]
def changeset(model, params \\ :empty) do
  model
  |> cast(params, ~w(name username), []) # WTF is ~w? Never explained
  |> validate_length(:username, @username_validation_length)
end

Why the ~w/1 function and this strange name? What does name/1 do?
When I look up the docs for cast/3 , I can just replace ~w/1 with [:name, :username] but I wan’t to know what’s the difference (besides eye cancer).

Another problem is the following code:

[web/controllers/auth.ex]
def call(conn, repo) do
  user_id = get_session(conn, :user_id)
  user = user_id && repo.get(Rumbl.User, user_id) # && should return boolean
  assign(conn, :current_user, user)
end

The docs are not really helpful in describing get_session/2:

get_session(conn, key)
Returns session value for the given key

I can assume that get_session/2 returns an int (but not because of the docs..).
I dont know what’s going on on the other lines of code, he is using this session id to get a user from the repo, but I thought the session id would be some kind of random value…
After that, I dont know whats going on with user (an int) and the returned struct…He is comparing them somehow…how is this working?
Thanks for taking time and reading this, I hope you can help me.

First 6 of 6 Posts Switch mode

OvermindDL1

OvermindDL1

~w is a sigil, it calls the macro named sigil_w at compile-time, which takes what is between the (/) pairs and parses it out via space separation into atoms in a list. It is explained at: https://elixir-lang.org/getting-started/sigils.html :slight_smile:

Basically ~w(name username) === [:name, :username]

No, I don’t get the point either, I just use atoms straight. I personally think it is a perfectly good wasted sigil name (we only have a limited amount after all!). ^.^;

NobbZ

NobbZ

Thats not a function, thats a “sigil”, in this case a word list, meaning it is equivalent to ["name", "username"] and very convienient.

And also its not /1, sigils are /2 :wink:

which is equivalent to ~w[name username]a (the a is an option to the w sigil and makes it return a list of atoms instead of strings, Another option is c which makes it return a list of char-lists.

The docs tell you, it can return any and totally depends on what you put there first.

After put_session(conn, :foo, "bar") you will get "bar" as a result of get_session(conn, :foo) on subsequent requests.


But to be honest, I’m sure that all of this is explained in the book before using it or very near to that snippet referencing it.

OvermindDL1

OvermindDL1

It returns whatever you put in to the session with put_session/3 in the same docs. :slight_smile:

He is just getting back whatever he put in. The ‘session’ is just storage for anything (keep it small) for a session. :slight_smile:

Not comparing, Matching. :slight_smile:

Rumbl.User is an atom, the module name specifically. get_session(conn, :user_id) is just getting out whatever was put in the session earlier via put_session/3, then matching it to user_id (so user_id is bound to the result of that). The repo.get(Rumbl.User, user_id) is looking up the Rumbl.User user with the primary key of whatever user_id is bound to at this time. The user_id && repo... means only run the second expression after the && if the expression to the left of it is not nil or false (I wish it tested [] too as that is really nil in my opinion… but it does not). All that is bound to user then, which is then assigned on the conn for the current connection via assign. :slight_smile:

NobbZ

NobbZ

NO!!! Its not! Without options given ~w creates a list of strings, not atoms. But yes, in the case of ectos cast/* they are treated identically, but they aren’t.

I don’t use it often as well, but I’ve seen it often used by folks comming from ruby where the equivalent %w() is more than common.

OvermindDL1

OvermindDL1

Er yes, that. ^.^

I just don’t really see what it saves, a single character per atom/word in exchange for one extra overall does not seem like a huge savings to make it worth using up one of the valuable/limited sigil names (I wish they were not restricted to a single character, don’t know why they are…).

NobbZ

NobbZ

As I said, its often used by the ruby folks, and please remember where José comes from :slight_smile:

Also I think it might come in handy when you have a lot of words which contain quotes or are IPs/hosts:

~w[foo"bar bar'baz ::1 example.com]a

If you have a lot of them and try it “traditionally” you’ll have some overhead I’d guess. But in simple cases as casts arguments I prefer explicit list of atoms as well.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement