[erlang-questions] Matching Empty maps

Raimo Niskanen <>
Tue Feb 7 15:48:33 CET 2017


On Tue, Feb 07, 2017 at 12:45:15PM +0100, Joe Armstrong wrote:
> Ten minutes ago I was happily recursing down a list and discovered
> to my horror that an empty map isn't an empty map.
> 
> 1> #{} = #{a => 1}.
> #{a => 1}
> 
> The documentation actually says:
> 
> <quote>
> #{} = Expr
> 
> This expression matches if the expression Expr is of type map,
> otherwise it fails with an exception badmatch.
> </quote>
> 
> So #{} is an empty map in an argument position but not an empty map
> in a pattern.
> 
> So this is not a bug since the system does what the documentation says
> but it is very weird.
> 
> Is there some deep reason for this that I've missed ??????
> 
> Very Puzzled

I think it is quite logical since #{a := _} in a match position matches a
map with a key 'a' plus any number of other keys in it, so why should not
the empty map #{} in a match position also match any number of other keys
in it?

> 
> /Joe

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB


More information about the erlang-questions mailing list