quantumpotato
quantumpotato

Reputation: 9767

Matching maps in Erlang

How can I match like this in Erlang?

#{o => 0} = maps:put(o, 0, maps:new()).
"Illegal pattern"

From what I can read in the erlang shell, the values are equivalent. Why can I not match?

Upvotes: 1

Views: 579

Answers (1)

Pascal
Pascal

Reputation: 14042

#{o := 0} = maps:put(o, 0, maps:new()).

The sign => is used to create a key value pair, the sign := works on a existing key. so the previous expression is valid for pattern matching, and the following is valid for comparison (both sides of the comparison are equivalent):

#{o => 0} == maps:put(o, 0, maps:new()).


1> #{o := 0} = #{o => 0,i => 1}. % will match                
#{i => 1,o => 0}
2> #{o := 0} = #{o => 2,i => 1}.  % will not match
** exception error: no match of right hand side value #{i => 1,o => 2}
3> #{o => 0} == #{o => 0,i => 1}. % is false
false
3>

Upvotes: 5

Related Questions