-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Map.map() allows for struct, now deprecated, suggests Map.new() which doesn't support struct #11912
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Can you please provide an example of a struct where calling Map.map makes sense? Because it may actually be an argument for keeping Map.map around. Thanks. |
Sure so currently in my use case I have a Struct that I need to rewrite some values on, the normal use case for record
|> Map.map(&transform_special_values/1)
|> Map.take(&allowed_fields/1) Other Something I'm not sure of that maybe you can answer: If I take a If yes, then I guess in my code I can do: %X{}
|> Map.from_struct()
|> Map.new(&a/1)
|> fn attributes -> struct(X, attributes) end.() even though that seems kinda cringe for just one function out of many. (Next proposal: |
Yes, because all it matters are the keys and values. There is no underlying identity or objects. So what you see is what you get. My concern with your approach is that you are traversing all struct keys only to modify potentially a few of them. It feels very inefficient. The whole traversal for structs feels a bit backwards. Wouldn't it be better to pattern match on the desired struct fields and the update them? %{a: a, b: b} = struct
...
%{struct | a: new_a, b: new_b} |
In my particular case I don't know the keys, thus the need for traversing. For the code in question I am looking to replace special values with "real" values in a broad list of structs which have different keys. The exact use case is a random data generator where I need to differentiate between empty values and values that are empty. I could explain the business logic further, but for the difference is |
Thank you! In this case, my suggestion might perhaps be something like this: fields = Map.from_struct(struct)
Enum.reduce(fields, struct, fn {k, v}, acc ->
# maybe or maybe not update acc
end) I think it makes the idea more explicit that you are simply updating the struct compared to something like |
Okay, so do I make a PR to add the detail to the deprecation notice that for version 1.13.0 and 1.13.1 |
The updated deprecation message already covers it: 9bb1202#diff-17d8120ee6e35ed2a465a5e10985eeb91b64f54b38347daf429cc20d1c85df85R1109. |
Ah-ha! Thanks I didn't notice the commit. |
Elixir and Erlang/OTP versions
Erlang/OTP 24 [erts-12.2.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit]
Elixir 1.13.1 (compiled with Erlang/OTP 24)
Operating system
Linux DESKTOP-968KC2R 5.4.72-microsoft-standard-WSL2 #1 SMP Wed Oct 28 23:40:43 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
Current behavior
Expected behavior
If Elixir is going to tell me to use
Map.new/2
instead ofMap.map/1
then I expectMap.new/2
to work with my value like it does withMap.map/1
.The text was updated successfully, but these errors were encountered: