I have two dictionaries:
d1 = Dict(:name => "Fido")
d2 = Dict(:name => "Spot", :breed => :poodle)
Which I’d like to use to create two
using Parameters
@with_kw mutable struct Dog
name::String
breed::Symbol = :husky
end
objects.
I presumed the splat operator would achieve this as it’s equivalent would in python, but this does not work
julia> Dog(d1...)
ERROR: MethodError: no method matching Dog(::Pair{Symbol, String})
Closest candidates are:
Dog(::Any, ::Any) at ~/.julia/packages/Parameters/MK0O4/src/Parameters.jl:505
Dog(; name, breed) at ~/.julia/packages/Parameters/MK0O4/src/Parameters.jl:493
Dog(::Dog; kws...) at ~/.julia/packages/Parameters/MK0O4/src/Parameters.jl:569
...
Stacktrace:
[1] top-level scope
@ REPL[35]:1
julia> Dog(d2...)
ERROR: MethodError: Cannot `convert` an object of type Pair{Symbol, Any} to an object of type String
Closest candidates are:
convert(::Type{String}, ::String) at essentials.jl:218
convert(::Type{T}, ::T) where T<:AbstractString at strings/basic.jl:231
convert(::Type{T}, ::AbstractString) where T<:AbstractString at strings/basic.jl:232
...
Stacktrace:
[1] Dog(name::Pair{Symbol, Any}, breed::Pair{Symbol, Any})
@ Main ~/.julia/packages/Parameters/MK0O4/src/Parameters.jl:505
[2] top-level scope
@ REPL[36]:1
How can I easily construct parameters objects from Dict?