erlangerlang-shell

Pattern matching the fields of a map in erlang


I'm currently reading book << Programming Erlang, 2nd edition >>. When I looked through the pattern matching of Map's field, the code snippet in the book complains some error on my Erlang prompt.

%% Book's version
1> Henry8 = #{ class => king, born => 1491, died => 1547 }. 
#{ born => 1491, class=> king, died => 1547 }.
2> #{ born => B } = Henry8.
#{ born => 1491, class=> king, died => 1547 }.
3> B.
1491

%% My Eshell V6.2
65> Henry8 = #{ class => king, born => 1491, died => 1547 }.
#{born => 1491,class => king,died => 1547}
66> #{ born => B } = Henry8.
* 1: illegal pattern

Is there anything that I'm missing? Thanks in advance.


Solution

  • Use := instead of =>

    #{ born := B } = Henry8.
    

    source: http://erlang.org/doc/reference_manual/expressions.html#maps-in-patterns