proposal: Map.replace/3

41 views
Skip to first unread message

Kensei N

unread,
Feb 22, 2020, 4:49:12 AM2/22/20
to elixir-lang-core
Hi !

I propose Map.replace/3.

Map.replace!/3 already exists.
However, it is difficult to use pattern match with Map.replace!/3  because it raise error when key is not present in map.

So, I think it is a good idea make Map.replace/3  and it return
:error
when 
key is not present in map, and it return
{:ok, %{hoge: "hoge"}}
when key is present and work correctly.

Example:
 iex> ex_map = %{a: 1, b: 2}
%{a: 1, b: 2}

 iex>Map.replace(ex_map, :a, 3)
{:ok, %{a: 3, b: 2}}

 iex>Map.replace(ex_map, :hoge, 3)
:error

Fernando Tapia Rico

unread,
Feb 22, 2020, 5:09:08 AM2/22/20
to elixir-l...@googlegroups.com
Hi Kensei,

Here is a bit of context :)

There was a Map.replace/3 (with a different behavior) that was deprecated in favor of Map.fetch/2 + Map.put/3. Here is a link to the code: 

Here is the rationale behind the deprecation: 

Hope this helps :)

--
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 elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/812de3be-6088-41ff-b9fa-6387d86ab1df%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages