How to think about pattern matching vs. type-checking

Yup, makes sense! So

%{x: a} = %{} = %{x: 1, y: 2}

is like:

%{} = %{x: 1, y: 2}

followed by:

%{x: a} = %{x: 1, y: 2}
1 Like

Something I didn’t see mentioned, which is worth knowing, is that when you match on a struct, you are really just telling the beam to match a map with a particular value in an entry with the key __struct__.

e.g.

def update_user(%User{} = user, updates) do

Is equivalent to

def update_user(%{__struct__: MyApp.User}  = user, updates) do
4 Likes

Great clarification. I was wondering why this works, since on one hand there always are default values when a struct is defined, on the other hand they seem to be ignored in this pattern except for the struct field.