Updating structs: Map.put vs %Foo{oldfoo | new: value} vs put_in

+1 to put existing.

1 Like

I was also considering update_value, but I think put_existing is better.

1 Like

Please send a PR. Probably put_new!, put_existing and put_existing!.

5 Likes

All right, will do :slight_smile:

3 Likes

I love you, people!

3 Likes

Here we are: PR #5917

3 Likes

put_in(struct, [Access.key!(:field)], 5) is what put_in(struct, ~a[field], 5) uses under the hood, by the way.

2 Likes

These changes have been pushed into the Elixir repository. Map.replace/3, Map.replace!/3, Keyword.replace/3 and Keyword.replace!/3 have been added. :blush:

4 Likes

I have just cycled back to Google -> ElixirForum -> this issue -> oh I was discussing this -> Map.replade! works when I already forgot how to do this. :smiley:

4 Likes

This was a really interesting discussion (and the PR as well).

For completeness here is a link to the commit where Map.replace/3 and Keyword.replace/3 were deprecated (which means they’re no longer documented and don’t show up if you try to do h Map.replace/3 in iEX).

3 Likes

@josevalim I believe it would had been much nicer if we have added the name Map.put! instead of the verb Map.replace! which has no corresponding function in Map :thinking:

I logged in just to say thank you for this. :smiley:

2 Likes