From Named Tuple to Struct

Say I have a Named Tuple like

a = (switch = dataframe1, swatch = dataframe2)

and I have some structs

struct switch
  df::DataFrame
  ...more fields
end
struct swatch
  df::DataFrame
  ...more fields
end

I want to iterate over the Named Tuples and create the corresponding structs.

What would be the best way to pick the right constructor for the struct corresponding to the key in the Named Tuple (I actually want to create one switch/swatch from each row in the dateframes in the Named Tuple, and in the actual problem there are also swutch, swytch and many more. :smiley: )

Is this a sufficient hint?

julia> module Foo

       struct switch
           x::Int
       end

       get_struct(name) = getfield(Foo, name)
       end
Main.Foo

julia> Foo.get_struct(:switch)(1)
Main.Foo.switch(1)
1 Like

I think so…

I have thought of getfield as getting values of fields in structs, but I guess a Module also has fields (a namespace?) of Symbols that can be used as the literal name itself.

If I understand correctly then fieldnames(Foo.get_struct(:switch)) should yield :x?

I was thinking of using dispatching on Val{:switch} or something but this seems more elegant and direct.