Destructuring syntax in julia 1.7

Wanted to destructure a dictionary, found this convenient:

mydict = Dict(:x=>1, :y=>2, :z=>3)
(; x, y) = NamedTuple(mydict)

Unfortunately it works only for dictionaries whose keys are symbols, Dict{Symbol, T}. It doesn’t work for a dictionary whose keys are strings Dict{String, T} (as is frequently the case for config files, etc.).

This can be addressed by overloading NamedTuple to convert string keys to symbols:

NamedTuple(d::Dict{String,T} where T) = NamedTuple{Tuple(Symbol(k) for k ∈ keys(d))}(v for v ∈ values(d))
2 Likes