[erlang-questions] pattern matching with maps in function heads

Martin Koroudjiev mrtndimitrov@REDACTED
Thu Oct 29 16:33:06 CET 2015


Hello,

How can I do pattern matching with maps in function heads? I tried

-module(m1).

-export([main/0]).

main() ->
    M = #{a => 1, b => 2, c => 3},
    io:format("b: ~p~n", get_attr(b, M)),
    io:format("d: ~p~n", get_attr(d, M)).

get_attr(Attr, #{Attr := Val} = Map) ->
    Val;
get_attr(_Attr, _M) ->
    not_found.

but I get compile error: m1.erl:10: variable 'Attr' is unbound

Thanks in advance,
Martin




More information about the erlang-questions mailing list