# Head/tail pattern with NamedTuple

**URL:** <https://discourse.julialang.org/t/head-tail-pattern-with-namedtuple/17554>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 15, 2018, 10:53am UTC](https://discourse.julialang.org/t/head-tail-pattern-with-namedtuple/17554 "2018-11-15T10:53:14Z")\
**Posts on this page:** 3\
**Page:** 1

<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 15, 2018, 10:53am UTC](https://discourse.julialang.org/t/head-tail-pattern-with-namedtuple/17554/1 "2018-11-15T10:53:14Z")

</div>

I want to create `NamedTuple`s from a parametric type where the parameter is the desired type. Fields are computed using fieldtypes (in the MWE below, `makezero`). From reading code in `Base`, I came up with the following solution, is this the right way?

```julia
# make a field -- note that this is just for the MWE
makezero(::Type{T}) where T = zero(T)

"Make a `T` of zeros."
struct ZeroMaker{T <: Union{Tuple, NamedTuple}} end

_makezeros(::Type{Tuple{}}) = ()
_makezeros(::Type{T}) where T <: Tuple =
    (makezero(Base.tuple_type_head(T)), _makezeros(Base.tuple_type_tail(T))...)

# this is a pattern I was familiar with
makezeros(::ZeroMaker{T}) where T <: Tuple = _makezeros(T)

# I am asking the question about this part
makezeros(::ZeroMaker{NamedTuple{N,S}}) where {N, S} =
    NamedTuple{N}(_makezeros(S))

```

This works fine, is there anything I could do simpler/differently?

```julia
julia> T = Tuple{Int, Float64}
Tuple{Int64,Float64}

julia> NT = NamedTuple{(:a, :b), T}
NamedTuple{(:a, :b),Tuple{Int64,Float64}}

julia> @code_typed makezeros(ZeroMaker{T}())
CodeInfo(
 1 1 ─ return (0, 0.0) │
) => Tuple{Int64,Float64}

julia> @code_typed makezeros(ZeroMaker{NT}())
CodeInfo(
 1 1 ─ return $(QuoteNode((a = 0, b = 0.0))) │
) => NamedTuple{(:a, :b),Tuple{Int64,Float64}}

```

---

<div class="post-metadata">

**Author:** ![oliver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliver/32/19264_2.png) [@oliver](https://discourse.julialang.org/u/oliver)\
**Post date:** [December 9, 2018, 2:06am UTC](https://discourse.julialang.org/t/head-tail-pattern-with-namedtuple/17554/2 "2018-12-09T02:06:05Z")

</div>

Did you ever find a better solution to this?

---

<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:** [December 9, 2018, 6:59am UTC](https://discourse.julialang.org/t/head-tail-pattern-with-namedtuple/17554/3 "2018-12-09T06:59:57Z")

</div>

No, this is perfect. You can see it in action in eg [StataDTAFiles.jl](https://github.com/tpapp/StataDTAFiles.jl/blob/53239b78cbb78969d05cb92055129da9863e2101/src/StataDTAFiles.jl#L297). Yields very efficient code.
