# How do I best convert a nested named tuple to a nested struct?

**URL:** <https://discourse.julialang.org/t/how-do-i-best-convert-a-nested-named-tuple-to-a-nested-struct/64001>\
**Category:** General Usage\
**Tags:** data\_structures, struct, namedtuple\
**Created:** [July 3, 2021, 7:35pm UTC](https://discourse.julialang.org/t/how-do-i-best-convert-a-nested-named-tuple-to-a-nested-struct/64001 "2021-07-03T19:35:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Post date:** [July 3, 2021, 7:35pm UTC](https://discourse.julialang.org/t/how-do-i-best-convert-a-nested-named-tuple-to-a-nested-struct/64001/1 "2021-07-03T19:35:36Z")

</div>

The code below contains two examples: the first example works, the second one doesn’t. The first example creates a struct, then writes it to an arrow file (which converts it to a named tuple along the way). Reading it back works fine.

In the second example, there is a vector of structures structure nested inside the outside structure. This does not work, because the inside argument is still a tuple, not a structure. What would be the best way of getting this to work? Write new constructors? Thanks!

```julia
using Arrow, DataFrames

struct Mine
    x::Int  
    y::Vector{Int}
    z::String
end

m1 = Mine( 0, [1;2], "one" )
m2 = Mine( 1, [4;6], "two" )
df = DataFrame( m = [m1;m2] )
Arrow.write( "mine.arrow", df )
df2 = Arrow.Table( "mine.arrow" ) |> DataFrame

println( typeof( df2[1,:m] ) )
println( Mine( df2[1,:m]... ) )

# the above works

struct Another
    a::String
    b::Vector{Mine}
end

m3 = Mine( 0, [1;2;8], "three" )

a1 = Another( "first", [m1;m2] )
a2 = Another( "second", [m2;m3] )

dfa = DataFrame( a = [a1; a2] )
Arrow.write( "another.arrow", dfa)
dfa2 = Arrow.Table( "another.arrow" ) |> DataFrame

println( dfa2[1,:a] )
# how best to do what is intended below?
println( Another( dfa2[1,:a]... ) )

```

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [July 4, 2021, 4:26am UTC](https://discourse.julialang.org/t/how-do-i-best-convert-a-nested-named-tuple-to-a-nested-struct/64001/2 "2021-07-04T04:26:04Z")

</div>

Check out [https://github.com/JeffreySarnoff/NamedTupleTools.jl#struct-construction-conversion](https://github.com/JeffreySarnoff/NamedTupleTools.jl#struct-construction-conversion)

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [July 4, 2021, 4:31am UTC](https://discourse.julialang.org/t/how-do-i-best-convert-a-nested-named-tuple-to-a-nested-struct/64001/3 "2021-07-04T04:31:39Z")

</div>

> [@Joris\_Pinkse](#):
>
> ```julia
> struct Mine
> x::Int  
> y::Vector{Int}
> z::String
> end
> 
> m1 = Mine( 0, [1;2], "one" )
> m2 = Mine( 1, [4;6], "two" )
> 
> ```

You could also recreate `nt2struct` something like this

```julia
struct Mine
    x::Int  
    y::Vector{Int}
    z::String
end

m1 = Mine( 0, [1,2], "one" )

function nt2struct(nt, ::Type{T}) where T
    fns = fieldnames(T)
    fs = getfield.((nt, ), fns)
    T(fs...)
end

nt = (x=0, y=[1,2], z="one")
m2 = nt2struct(nt, Mine)

```

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [July 4, 2021, 4:42am UTC](https://discourse.julialang.org/t/how-do-i-best-convert-a-nested-named-tuple-to-a-nested-struct/64001/4 "2021-07-04T04:42:17Z")

</div>

Edit: This version is a bit faster than the one above, but still not completely type stable

```julia
function nt2struct(nt, ::Type{T}) where T
    fns = fieldnames(T)
    fs = ntuple(i->getfield(nt, fns[i]), fieldcount(T))
    T(fs...)
end

```

Edit2: This version is type stable and fast

```julia
function nt2struct(nt::NamedTuple{names, types}, ::Type{T}) where {names, types, T}
    fs = ntuple(i->getfield(nt, names[i]), fieldcount(T))
    T(fs...)
end

```

---

<div class="post-metadata">

**Author:** ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Post date:** [July 5, 2021, 1:49pm UTC](https://discourse.julialang.org/t/how-do-i-best-convert-a-nested-named-tuple-to-a-nested-struct/64001/5 "2021-07-05T13:49:39Z")

</div>

Thanks @baggepinnen !

I had managed to convert a named tuple to a string (top half); it is the nested part that I’m having trouble with (bottom half).

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [July 5, 2021, 4:03pm UTC](https://discourse.julialang.org/t/how-do-i-best-convert-a-nested-named-tuple-to-a-nested-struct/64001/6 "2021-07-05T16:03:51Z")

</div>

Thank you for the (to be credited) contribution to NamedTupleTools v2!
