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.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 6 of 6 Posts
OvermindDL1
~wis a sigil, it calls the macro namedsigil_wat 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.htmlBasically
~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
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/2which is equivalent to
~w[name username]a(theais an option to thewsigil and makes it return a list of atoms instead of strings, Another option iscwhich makes it return a list of char-lists.The docs tell you, it can return
anyand totally depends on what you put there first.After
put_session(conn, :foo, "bar")you will get"bar"as a result ofget_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
It returns whatever you put in to the session with
put_session/3in the same docs.He is just getting back whatever he put in. The ‘session’ is just storage for anything (keep it small) for a session.
Not comparing,
Matching.Rumbl.Useris an atom, the module name specifically.get_session(conn, :user_id)is just getting out whatever was put in the session earlier viaput_session/3, then matching it touser_id(souser_idis bound to the result of that). Therepo.get(Rumbl.User, user_id)is looking up theRumbl.Useruser with the primary key of whateveruser_idis bound to at this time. Theuser_id && repo...means only run the second expression after the&&if the expression to the left of it is notnilorfalse(I wish it tested[]too as that is really nil in my opinion… but it does not). All that is bound touserthen, which is then assigned on the conn for the current connection viaassign.NobbZ
NO!!! Its not! Without options given
~wcreates a list of strings, not atoms. But yes, in the case of ectoscast/*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
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
As I said, its often used by the ruby folks, and please remember where José comes from
Also I think it might come in handy when you have a lot of words which contain quotes or are IPs/hosts:
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.