Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass arguments to @kwdef struct programmatically

Tags:

Pass arguments to @kwdef struct programmatically

The issue

I have this struct:

Base.@kwdef struct example_struc
    Latitude::Float64  = 9.9        # Latitude (degree)
    Longitude::Float64 = -83.7      # Longitude (degree)
end

@kwdef allows me to instantiate an example_struc() without giving all arguments thanks to the defaults, e.g.:

julia> a= example_struc(Longitude= 40.0)
julia> a.Latitude
9.93833
julia> a.Longitude
40.0

I would like to instantiate it programmatically (from a tuple read in a file), by passing to example_struc the name of the argument, and its value.

What I tried

What works

I can do it for one argument using metaprograming like this:

# Named tuple usually read from a file:
params= (Latitude = 43.61, Longitude = 3.877)
params_names= collect(keys(params))
lat= :($(params[1]))
lat_name= :($(params_names[1]))
e= :(example_struc($(lat_name)= $(lat)))
a= eval(e)

e looks like this :(example_struc(Latitude=43.61)), and a is exactly as before.

What doesn't work

Now in my case the number of arguments is more than two (up to 50), so I need to be able to do that for multiple arguments at once. So I tried passing the function arguments as a whole using map:

b= map((x,y) -> :($x = $y),params_names,params)
f= :(example_struc($(b...)))
eval(f)

f looks like this: :(example_struc(Latitude = 43.61, Longitude = 3.877)) And it works, but only because we pass all the arguments: we are not using the defaults.

Now if I want to use a default value for Longitude, it doesn't work:

b= map((x,y) -> :($x = $y),[params_names[1]],[params[1]])
f= :(example_struc($(b...)))
eval(f)

f looks like this: :(example_struc(Latitude = 43.61)) But now there is an error : ERROR: MethodError: no method matching example_struc(::Float64)

So instead of calling the function like I would expect like this: example_struc(Latitude = 43.61), it calls it like this: example_struc(43.61), without the parameter name.

Any idea on how to fix this ? I am open to any solution, including changing the way the user gives the inputs (but it has to be simple).

More background

I'm writing a program in Julia that read a user input file that possibly have Julia code in it (it is safe because the user only use it locally). So the input file is a .jl file itself that is evaluated using evalfile, and the user provide the parameter values in a Tuple, e.g.:

(
 Latitude::Float64  = 9.9,        # Latitude (degree)
 Longitude::Float64 = -83.7       # Longitude (degree)
 some_function= x -> x + 2        # Some functions can be defined by the user (if not, we use the default definition)
)

My program reads the Tuple, and I would like to provide some default values, e.g. if the user only put the Latitude, the program uses a default Longitude and default some_function. To do so, I use a @kwdef struct to leverage its default capabilities, but I need to know how to pass the arguments programmatically.

like image 554
rvezy Avatar asked Jul 19 '19 08:07

rvezy


1 Answers

You should be able to just unpack a named tuple into the keyword argument position of the constructor. Here's a minimal example:

julia> Base.@kwdef struct A
           x::Int64 = 1
           y::Int64 = 2
           z::Int64 = 3
       end
A

julia> kwargs = (z = 5, y = 4)
(z = 5, y = 4)

julia> A(; kwargs...)
A(1, 4, 5)

Note that you need to use the semicolon in the function call to indicate that the unpacked arguments are keyword arguments. Without the semicolon, you'll get a method error:

julia> A(kwargs...)
ERROR: MethodError: no method matching A(::Int64, ::Int64)
Closest candidates are:
  A(::Int64, ::Int64, ::Int64) at REPL[13]:2
  A(::Any, ::Any, ::Any) at REPL[13]:2
Stacktrace:
 [1] top-level scope at none:0

See here for more details on keyword arguments.

like image 73
Cameron Bieganek Avatar answered Oct 04 '22 08:10

Cameron Bieganek