as a side note, you can note that erlang has :maps.put (inserts/replace a 
k/v) and :maps.update (only replaces an existing kv),

elixir mimics this by having Map.put for insert/replace and %{x | atom: 
:atom} for replace (note that Map.update/4 has a different behaviour and is 
too complex for this case where you simply want to set an specific value to 
a map that should have the expected key)

On Tuesday, January 16, 2018 at 2:56:10 PM UTC-2, [email protected] 
wrote:
>
> 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/18f0856a-91df-47ec-a115-aed5367bb67c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to