# From Named Tuple to Struct

**URL:** <https://discourse.julialang.org/t/from-named-tuple-to-struct/127927>\
**Category:** New to Julia\
**Tags:** struct, namedtuple\
**Created:** [April 10, 2025, 12:33pm UTC](https://discourse.julialang.org/t/from-named-tuple-to-struct/127927 "2025-04-10T12:33:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Magnus\_Lilledahl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magnus_lilledahl/32/51079_2.png) [@Magnus\_Lilledahl](https://discourse.julialang.org/u/Magnus_Lilledahl)\
**Post date:** [April 10, 2025, 12:33pm UTC](https://discourse.julialang.org/t/from-named-tuple-to-struct/127927/1 "2025-04-10T12:33:35Z")

</div>

Say I have a Named Tuple like

```julia
a = (switch = dataframe1, swatch = dataframe2)

```

and I have some structs

```julia
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. 😃 )

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [April 10, 2025, 2:03pm UTC](https://discourse.julialang.org/t/from-named-tuple-to-struct/127927/2 "2025-04-10T14:03:45Z")

</div>

Is this a sufficient hint?

```julia
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)

```

---

<div class="post-metadata">

**Author:** ![Magnus\_Lilledahl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magnus_lilledahl/32/51079_2.png) [@Magnus\_Lilledahl](https://discourse.julialang.org/u/Magnus_Lilledahl)\
**Post date:** [April 10, 2025, 2:35pm UTC](https://discourse.julialang.org/t/from-named-tuple-to-struct/127927/3 "2025-04-10T14:35:24Z")

</div>

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.
