# How to define NamedTuple types in struct?

**URL:** <https://discourse.julialang.org/t/how-to-define-namedtuple-types-in-struct/31631>\
**Category:** New to Julia\
**Created:** [November 28, 2019, 5:08pm UTC](https://discourse.julialang.org/t/how-to-define-namedtuple-types-in-struct/31631 "2019-11-28T17:08:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elmer\_Cusipuma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmer_cusipuma/32/11958_2.png) [@Elmer\_Cusipuma](https://discourse.julialang.org/u/Elmer_Cusipuma)\
**Post date:** [November 28, 2019, 5:08pm UTC](https://discourse.julialang.org/t/how-to-define-namedtuple-types-in-struct/31631/1 "2019-11-28T17:08:45Z")

</div>

I can implement the next code:

```julia
struct data
    p::NamedTuple{(:a,:b,:c,:d,:e), Tuple{Symbol,Bool,Any,Any,Any}}
end

q1 = data((a=:n,b=true,c="one",d=1,e=1.2))
q2 = data((a=:n,b=false,c="one",d=3.5,e=nothing))
#I get
julia> typeof(q1.p)
NamedTuple{(:a, :b, :c, :d, :e),Tuple{Symbol,Bool,Any,Any,Any}}

typeof(q2.p)
NamedTuple{(:a, :b, :c, :d, :e),Tuple{Symbol,Bool,Any,Any,Any}}

```

**(a)** How can I group the structure types without using abstract types? I am hoping to get:

```julia
# Expected result
julia> typeof(q1.p)
NamedTuple{(:a, :b, :c, :d, :e),Tuple{Symbol,Bool,String,Int64,Float64}}

julia> typeof(q2.p)
NamedTuple{(:a, :b, :c, :d, :e),Tuple{Symbol,Bool,String,Float64,Nothing}}

```

**(b)** Is there any way to define only the first two parameters?

```julia
struct data
    p::NamedTuple{(:a,:b,:c,:d,:e), Tuple{Symbol,Bool,...}}
end

```

---

<div class="post-metadata">

**Author:** ![sairus7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sairus7/32/10816_2.png) [@sairus7](https://discourse.julialang.org/u/sairus7)\
**Post date:** [November 28, 2019, 6:10pm UTC](https://discourse.julialang.org/t/how-to-define-namedtuple-types-in-struct/31631/2 "2019-11-28T18:10:11Z")

</div>

You can parametrize your type:

```julia
struct data{T1,T2,T3}
    p::NamedTuple{(:a,:b,:c,:d,:e), Tuple{Symbol,Bool,T1,T2,T3}}
end

```

---

<div class="post-metadata">

**Author:** ![Elmer\_Cusipuma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmer_cusipuma/32/11958_2.png) [@Elmer\_Cusipuma](https://discourse.julialang.org/u/Elmer_Cusipuma)\
**Post date:** [November 29, 2019, 6:27pm UTC](https://discourse.julialang.org/t/how-to-define-namedtuple-types-in-struct/31631/3 "2019-11-29T18:27:50Z")

</div>

@sairus7 Now it’s works, thank you so much for your help.

I have another question  
I this case the structure has dictionaries

```julia
struct data{T}
    d::Dict{String,NamedTuple{(:a,:b,:c), Tuple{Symbol,T,T}}}
end

# the values of b and c are equal
dt = data( Dict( "p"=>(a=:n,b=1,c=1), "q"=>(a=:m,b=2,c=4) ) )

```

How can I parametrize the types so NameTuples can accept different type values?

e.g.

```julia
#the values of b and c are different
dt = data( Dict( "p"=>(a=:n,b=1.2,c=1.3), "q"=>(a=:m,b=nothing,c=nothing) ) )

```

My understanding is that it is possible to make a union of types `Union{Real,Nothing}`

---

<div class="post-metadata">

**Author:** ![wookyoung](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wookyoung/32/157_2.png) [@wookyoung](https://discourse.julialang.org/u/wookyoung)\
**Post date:** [November 29, 2019, 9:12pm UTC](https://discourse.julialang.org/t/how-to-define-namedtuple-types-in-struct/31631/4 "2019-11-29T21:12:51Z")

</div>

```julia
julia> struct data
           d::Dict{String,NamedTuple{(:a,:b,:c), <:Tuple{Symbol,T,T} where T}}
       end

julia> dt = data( Dict( "p"=>(a=:n,b=1.2,c=1.3), "q"=>(a=:m,b=nothing,c=nothing) ) )

data(Dict{String,NamedTuple{(:a, :b, :c),#s1} where #s1<:(Tuple{Symbol,T,T} where T)}("q" => (a = :m, b = nothing, c = nothing),"p" => (a = :n, b = 1.2, c = 1.3)))

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 30, 2019, 7:05am UTC](https://discourse.julialang.org/t/how-to-define-namedtuple-types-in-struct/31631/5 "2019-11-30T07:05:55Z")

</div>

> [@Elmer\_Cusipuma](#):
>
> My understanding is that it is possible to make a union of types `Union{Real,Nothing}`

If you know that you only need a concrete `<::Real` type and `Nothing`, you can do eg

```julia
const myNT{T} = NamedTuple{(:a,:b,:c), Tuple{Symbol,T,T}}

struct Data{T <: Real}
    d::Dict{String,Union{myNT{T},myNT{Nothing}}}
    Data{T}() where T = new{T}(Dict{String,Union{myNT{T},myNT{Nothing}}}())
end

dt = Data{Float64}()

dt.d["p"] = (a=:n,b=1.2,c=1.3)
dt.d["q"] = (a=:m,b=nothing,c=nothing)

```

but if `T` itself is abstract (eg `Real`) this is unlikely to result in performant code (this may not matter of course).
