Pattern matching in `case` for maps

I have a question on pattern matching in case. This function

def f(map) do
  case map do
    %{a: i} -> i
    %{} -> :empty
    _ -> :other
  end
end

works in this way:

> f(%{a: 67})
67
> f(%{b: 67})
:empty
> f(%{})
:empty

Can anybody explain me why it matches on the second clause, when non-empty map provided? I’m on Elixir 1.11.

1 Like

Hi @vitalydolgov :wave:t2: And welcome to the community!

%{} matches any map.

To match against empty map you could use either map_size/1 function or compare with %{} explicitly in guard as:

def f(map) do
  case map do
    # any map that has key :a
    %{a: i} -> i

    # empty map
    map when map == %{} -> :empty

    # map with exactly 1 key but not :a
    map when map_size(map) == 1 -> :one_key_not_a

    # any other map
    %{} -> :other

    # not a map
    _ -> :not_a_map
  end
end
4 Likes

Hey

But basically you just need to use a guard for the second case

1 Like

It is also in the hexdocs the reference for it, in the section on maps:

“Note that the empty map will match all maps,…”

https://hexdocs.pm/elixir/master/patterns-and-guards.html

2 Likes

Thank you :blush: