Hey everyone, I created and published a new library to deep merge maps and keyword lists. You can find it on github, hex and hexdocs. I also wrote a small release blog post detailing how the library came to be after trying to get deep_merge into elixir.
…but you can implement the simple DeepMerge.Resolver protocol for types/structs of your choice to also make them be deep mergable
a deep_merge/3 variant that gets a function similar to Map.merge/3 to modify the merging behavior, for instance in case you don’t want keyword lists to be merged or you want all lists to be appended
It might be my interpretation, but there seems to be a chance that something like deep_merge will be added to Elixir at some time in the future if it turns out to be more useful/popular/something:
I believe though there are still too many questions to be answered before moving forward.
and
(…) as long as you pick a sane namespace, like DeepMerge, it should be fine. It is unlikely we would add it to Elixir as DeepMerge.
All that said, I think a library is a perfectly fine place for this to live, thanks to hex it’s easy to add and use and I hope the library lives up to all your quality standards and if not please tell me
Plus, I have to commend the elixir team for rejecting features. Imo it’s a hard but very necessary thing to do to keep a project lean and maintainable
To be honest I was a bit surprised by the keyword list merging behavior as well, but basically this follows what Keyword.merge/2 does in Elixir (it uses Keyword.merge/3 underneath) and I want it to be the same and be consistent with whatever Elixir does because otherwise it’d just be weird and confusing.
That said, the mailing list discussion showed me that people have different ways they want deep_merge to work especially for lists/keyword lists so that might be a great use case for you for DeepMerge.deep_merge/3 and the reason why I didn’t publish until I had that worked out
To be honest I’m also a bit surprised!
I agree with your arguments, but I’m not satisfied.
I think I can add merge and reverse_merge (for sorting order) methods to my differ package that I’m currently working on it.
My merge methods will be based on diff so it will follow the second way that I asked about.
I see only this two ways to merge (elixir based and diff based). Is there other ways that developers need?
override behaviour (aka take the right side), at least for deep_merge as I could have a map with keywords as values but I don’t want them to be merged but rather treat them as values
domain logic - not something I’ve actively tried but I think if you think about ecto’s queries and their keywords you could come up with a number of valid ways to “merge” them (intersection, union, append, connect with OR/AND), although I don’t think it’s a good thing to ever do
given that duplicated keys are absolutely fine in keyword lists one might just wish to append - this is also true for merging “normal” lists (someone on the mailing list actually wanted this) - but it’s hard to distinguish as “normal” lists back keyword lists
I’m sure other people can and will come up with more ways