Skip to content

Commit

Permalink
Fix recursive_merge() to return same type as its first argument
Browse files Browse the repository at this point in the history
`mergewith()` always returns a `Dict`, ignoring the types of its
arguments. We need `recursive_merge()` to return an `OrderedDict`, so
re-implement `recursive_merge()` 'by hand' without using `mergewith()`.
  • Loading branch information
johnomotani committed Nov 13, 2024
1 parent 9b366f6 commit b8f52db
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions moment_kinetics/src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -312,16 +312,20 @@ Merge two AbstractDicts `a` and `b`. Any elements that are AbstractDicts are als
"""
function recursive_merge end
function recursive_merge(a::AbstractDict, b::AbstractDict)
return mergewith(recursive_merge, a, b)
end
function recursive_merge(a::AbstractDict, b)
error("Cannot merge a Dict with a non-Dict, got $a and $b")
end
function recursive_merge(a, b::AbstractDict)
error("Cannot merge a Dict with a non-Dict, got $a and $b")
end
function recursive_merge(a, b)
return b
result = deepcopy(a)
a_keys = collect(keys(a))
for (k,v) pairs(b)
if k a_keys
result[k] = v
elseif isa(result[k], AbstractDict) && isa(v, AbstractDict)
result[k] = recursive_merge(result[k], v)
elseif isa(result[k], AbstractDict) || isa(v, AbstractDict)
error("Cannot merge a Dict with a non-Dict, got $(result[k]) and $v")
else
result[k] = v
end
end
return result
end

"""
Expand Down

0 comments on commit b8f52db

Please sign in to comment.