Hi, is there an easy way to have Parameters.jl skip missing keys or fields?
using Parameters
mydict = Dict(a=>3,b=>2.3,c=>"mystr")
@unpack (a,b,c,d) = mydict
ERROR: KeyError: key "d" not found
I would like it to just skip over the missing key. The reason I want this behavior is I am carrying around two lists which hold fixed parameters and ones to be found via optimization. It would be nice if I could just unpack them, without having to check which ones were in which list like:
I don’t think so. Part of the reason @unpack is fast is because it doesn’t have these kinds of checks. A macro doesn’t know what’s inside a Dict.
You can, but you should import methods first.
But this is type piracy and shouldn’t be used if you are writing a library (but more or less fine in your local script)
julia> using UnPack
julia> import UnPack: unpack
help?> unpack
search: unpack UnPack @unpack
This function is invoked to unpack one field/entry of some DataType dt and has signature:
unpack(dt::Any, ::Val{sym}) -> value of sym
The sym is the symbol of the assigned variable.
Three definitions are included in the package to unpack a composite type or a dictionary with Symbol
or string keys:
@inline unpack(x, ::Val{f}) where {f} = getproperty(x, f)
@inline unpack(x::AbstractDict{Symbol}, ::Val{k}) where {k} = x[k]
@inline unpack(x::AbstractDict{S}, ::Val{k}) where {S<:AbstractString,k} = x[string(k)]
More methods can be added to allow for specialized unpacking of other datatypes.
See also pack!.
julia> unpack(x::AbstractDict{S}, ::Val{k}) where {S<:AbstractString,k} = get(x, string(k), nothing)
unpack (generic function with 3 methods)
julia> d = Dict(["a" => 1])
Dict{String, Int64} with 1 entry:
"a" => 1
julia> @unpack a, c = d
Dict{String, Int64} with 1 entry:
"a" => 1
julia> c
That said, don’t do this… Maybe you can define your own macro, use @unpack's parsing, but call your own function instead of unpack.