menw
Are these two maps different when doing pattern matching?
Hi, I’m trying to make a website with phoenix and using pattern matching of maps to pass datas.
So, I was trying to make a Map with the given data like this.
defp update_pw(user,new_pw) do
pw_param = %{password: new_pw, password_confirmation: new_pw}
IO.inspect(pw_param)
AccountContext.update_password(user,pw_param)
end
and throw it to update_password
def update_password(user_or_changeset, %{"password" => _, "password_confirmation" => _} = attrs) do
IO.inspect(attrs)
user_or_changeset
|> User.changeset_update_password(attrs)
|> Repo.update()
end
But the pattern Matching gave me an error. So I tried like this code
def update_password(user_or_changeset, %{password: _, password_confirmation: _} = attrs) do
IO.inspect(attrs)
user_or_changeset
|> User.changeset_update_password(attrs)
|> Repo.update()
end
and it worked… I thought %{“password” => _, “password_confirmation” => _} and %{password: _, password_confirmation: _} was same, but It wasn’t.
Was I understanding the pattern matching wrong?
Marked As Solved
imsoulfly
Hey,
I guess you understand pattern very well the only thing is you are trying to pattern match a "string" against an :atom
%{"password" => _, "password_confirmation" => _}
%{password: _, password_confirmation: _}
Elixir differentiates between the two as they are different types
Also Liked
mayel
One has atom keys and the other binary keys, not the same! %{password: _, password_confirmation: _} is shorthand for %{:password => _, :password_confirmation => _}
Popular in Questions
Other popular 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
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex










