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

itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
rlipscombe
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used? I’ve got two Github accoun...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews