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.

Showing Posts 1 to 6

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

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews