Skip to content

Conversation

@johnomotani
Copy link
Contributor

Fixes #77.

return dc
end

mergewith(combine::Function, d::LittleDict, others::AbstractDict...) = merge(combine, d, others...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Function is very restrictive, it excludes a bunch of useful applications, e.g. where combine is a type (so expressing coercion). However just making this Any can lead to ambiguities in the merge method. Avoiding that is AFAIK precisely the reason why mergewith was introduced in the first place.

So instead of implementing mergewith in terms of merge it is better to do it the other way around.

Thus I suggest that instead of adding this here, you instead rename the preceding merge method starting at line 169 to mergewith and drop the ::Function from its first argument.

Then here you can do

Suggested change
mergewith(combine::Function, d::LittleDict, others::AbstractDict...) = merge(combine, d, others...)
merge(combine::Function, d::LittleDict, others::AbstractDict...) = mergewith(combine, d, others...)

or even better (matching what the mergewith docstring says):

Suggested change
mergewith(combine::Function, d::LittleDict, others::AbstractDict...) = merge(combine, d, others...)
merge(combine::Union{Function,Type}, d::LittleDict, others::AbstractDict...) = mergewith(combine, d, others...)

merge!(combine, OrderedDict{K,V}(), d, others...)
end

mergewith(combine::Function, d::OrderedDict, others::AbstractDict...) = merge(combine, d, others...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above I suggest to generalize this so that combine can be anything. This should work:

Suggested change
mergewith(combine::Function, d::OrderedDict, others::AbstractDict...) = merge(combine, d, others...)
function mergewith(combine, d::OrderedDict, others::AbstractDict...)
K,V = _merge_kvtypes(d, others...)
mergewith!(combine, OrderedDict{K,V}(), d, others...)
end

Optionally the preceding merge method could then be turned into a one-liner calling mergewith.

@fingolfin
Copy link
Member

Just to be clear: I am not a maintainer of this project, feel free to ignore me. Also, regardless of anything else: thank you for making the effort and contributing to this package, much appreciated. (I should have started with that, sorry)

@johnomotani
Copy link
Contributor Author

Thanks @fingolfin, makes sense! I had no idea why mergewith existed in Julia, just that I was disappointed it didn't support OrderedDict. Will update as you suggested.

As suggested by @fingolfin, implementing `merge()` with a
`combine::Function` argument in terms of `mergewith()` allows the
signature of `mergewith()` to not restrict `combine` to be a `Function`
specifically, which can be useful in some situations.
Copy link
Member

@fingolfin fingolfin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thank you :-)

@fingolfin fingolfin merged commit fa5e681 into JuliaCollections:master Nov 22, 2024
1 check passed
@johnomotani johnomotani deleted the mergewith branch November 22, 2024 09:18
@fingolfin fingolfin mentioned this pull request Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mergewith not working properly for OrderedDict

2 participants