The syntax for updating maps (ie: `%{x | foo: :bar}`) assumes the key is 
already set on the map, this is to, not only, avoid bugs but to allow 
potential optimizations

In https://hexdocs.pm/elixir/Map.html you can see that this is an expected 
behaviour

> Maps also support a specific update syntax to update the value stored 
under *existing* atom keys:

On Tuesday, January 16, 2018 at 2:16:24 PM UTC-2, mG S wrote:
>
> Hi everyone, 
>
> Well the whole question fits in the subject...
> I was wondering why the pipe operator used in a map was sugar for 
> Map.update and not Map.put
>
> iex(1)> m=%{foo: 1}
> %{foo: 1}
> iex(2)> %{m | bar: 2}
> ** (KeyError) key :bar not found in: %{foo: 1}
>     (stdlib) :maps.update(:bar, 2, %{foo: 1})
>     (stdlib) erl_eval.erl:255: anonymous fn/2 in :erl_eval.expr/5
>     (stdlib) lists.erl:1262: :lists.foldl/3
> iex(2)> %{m | foo: 2}
> %{foo: 2}
>
>
> I'm sure it has good reasons, but I'm a little puzzled on why ?
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/035184ab-20cf-47d5-bb84-16ac43197e28%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to