[erlang-questions] Maps

Björn-Egil Dahlberg wallentin.dahlberg@REDACTED
Tue May 14 19:40:16 CEST 2013


2013/5/8 Fred Hebert <mononcqc@REDACTED>

> I was looking at an example:
>
>     1> M = #{ 1 => a },
>     #{ 1 => a }
>     2> M#{ 1.0 => b }.
>     #{ 1.0 => b }.
>     3> M#{ 1 := b }.
>     #{ 1 => b }
>     4> M#{ 1.0 := b }.
>     ** exception error: bad argument
>
> And given the definition where we have 'match' and 'equal' being two
> different things, am I right in understanding that '=>' can both create
> a mapping and update it based on 'equal' whereas ':=' can only update
> them using 'match' ?
>

Yes.
 '=>' is used to associate new key value pairs, or update an existing
(*equal*) key.
 ':=' is used to update an existing (matching) key, or used in patterns in
matching.


>
> I don't think there is any mention of the expected behaviour when
> matching on #{_ := V} = #{a => 3}, unless I missed it (there
> were cases in map comprehensions, but these are obviously not the same).
> Does this need to be specified? Similarly, #{_ := V} = #{a => 1, b => 1}
> and #{K := 1} = #{a => 1, b => 1} do not seem specified to me.
>

No, #{ _ := V } = #{ a => 1} is not allowed. Only keys which are bound by
the environment are allowed.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130514/cbf2ecbc/attachment.htm>


More information about the erlang-questions mailing list