[erlang-questions] pattern matching & map

Per Hedeland per@REDACTED
Mon Nov 26 13:26:43 CET 2018


Btw,
http://erlang.org/doc/reference_manual/expressions.html#maps-in-guards
lists is_map/1 and map_size/1, which are also documented as "Allowed in
guard tests" in the erlang(3) man page (a.k.a.
http://erlang.org/doc/man/erlang.html) - while the new-in-21 BIFs
map_get/2 and is_map_key/2 are neither listed nor so documented (only
the README mentions that they can be used in guards AFAICS).

--Per

On 2018-11-26 12:35, Per Hedeland wrote:
> On 2018-11-26 12:16, zxq9@REDACTED wrote:
>> On 2018t1126åÜå 14B1224Ò JST Oleg wrote:
>>> On Mon, Nov 26, 2018 at 11:12:39AM +0100, MichaB MuskaBa wrote:
>>>> In OTP 21 you could use the map_get/2 guard:
>>>>
>>>> add_prms([{Name, Val}|T], Prms) when map_get(Name, Prms) =:= Val ->
>>>
>>> Wow. That's great! But why we can't simply do:
>>>
>>> add_prms([{Name, Val}|T], #{Name := Vals} = Prms) ->
>>>
>>> :-)?
>>
>> IIRC is is because `Name` isn't really being used to match (unify) there, it is being used to call into map code, which is slightly different and only abstracted by the magical #{} syntax. There is an explanation of this somewhere on list, but I don't have the link handy.
> 
> AFAIR the "problem" is that the order of evaluation of function
> arguments is undefined. The #{Name := Vals} matching requires that
> 'Name' is bound (there is no "magic" AFAIK) - and it *would* be in
> the above function head, if argument evaluation was defined to be
> left-to-right.
> 
> --Per Hedeland
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
> 




More information about the erlang-questions mailing list