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?

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

1 Like

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

1 Like