menw

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

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

mayel

One has atom keys and the other binary keys, not the same! %{password: _, password_confirmation: _} is shorthand for %{:password => _, :password_confirmation => _}

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement