Composing new and default Kwargs

Hello,

I upgraded Julia from version 0.63 to version 1.4. To compose new and default Kwargs in Julia v0.63 I used the following function:

function foo(; kwargs...)
       defaults = (;  x="default")
       merge!(defaults, kwargs)
       return defaults
end

new = (;  x="new")
kwargs = foo(;x="new")

When I try to use the same method in Julia 1.4 the following message rises

MethodError: no method matching merge!(::NamedTuple{(:x,),Tuple{String}}, ::Base.Iterators.Pairs{Symbol,String,Tuple{Symbol},NamedTuple{(:x,),Tuple{String}}})

I suppose that the merge function is not well defined for this type of variable.

Do you suggest a way to do it?

Have you run your code on Julia 0.7? As that will give you all deprecation warnings with details on how to update the code.

1 Like

Sorry no I just went directly to 1.4. But you give me an Idea. @mauro3 Do you know if there is a way to run Julia 0.7 in the Web-browser??

NamedTuples are immutable, so you can’t use merge!. Instead use merge which will return a new NamedTuple:

function foo(; kwargs...)
       defaults = (;  x="default")
       merge(defaults, kwargs)
end
2 Likes

@ssfrr Thanks for your help now it works :smile: