# Type of Array of NamedTuples with same names but different tuples

**URL:** <https://discourse.julialang.org/t/type-of-array-of-namedtuples-with-same-names-but-different-tuples/53389>\
**Category:** New to Julia\
**Tags:** type, parametric-types\
**Created:** [January 15, 2021, 3:41pm UTC](https://discourse.julialang.org/t/type-of-array-of-namedtuples-with-same-names-but-different-tuples/53389 "2021-01-15T15:41:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fratrik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fratrik/32/678_2.png) [@fratrik](https://discourse.julialang.org/u/fratrik)\
**Post date:** [January 15, 2021, 3:41pm UTC](https://discourse.julialang.org/t/type-of-array-of-namedtuples-with-same-names-but-different-tuples/53389/1 "2021-01-15T15:41:20Z")

</div>

I want to write a function that iterates over arrays of named tuples. In particular, I want to write a map reduce that pulls the names up to the highest level:

```julia
function sel_map_red(nt::AbstractArray{NamedTuple{names, T}, N}; m=identity, r=identity) where {names, T, N}
    NamedTuple{names}(map(names) do n
        r(map(el->m(el[n]), nt))
    end)
end

```

This works fine for arrays where all the names have identical types:

```julia
b=[(a=1,), (a=2,)]
sel_map_red(b)

```

However it breaks if the elements of the array have the same names, but different types:

```julia
b = [(a=1,), (a=true,)]
sel_map_red(b)

```

I believe it has something to with Tuples not being covariant? [Broader (non-concrete) types in NamedTuple](https://discourse.julialang.org/t/broader-non-concrete-types-in-namedtuple/16183)

The first line evaluates to true, but the second line doesnt

```julia
typeof(b) <: Array{NamedTuple{names,T} where T<:Tuple,1} where {names}
typeof(b) <: Array{NamedTuple{names,T},1} where {names, T}

```

I can change the signature of sel\_map\_red to be the first line, but then it will work in the second example and not the first.

What I’m at a loss to do is to write a single type signature for sel\_map\_red that accepts both:

```julia
b=[(a=1,), (a=2,)]
b=[(a=1,), (a=true,)]

```

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [January 15, 2021, 4:51pm UTC](https://discourse.julialang.org/t/type-of-array-of-namedtuples-with-same-names-but-different-tuples/53389/2 "2021-01-15T16:51:12Z")

</div>

You could drop all the type annotations and extract the names like this:

```julia
function sel_map_red(nt; m=identity, r=identity)
    names = fieldnames(typeof(first(nt)))
    NamedTuple{names}(map(names) do n
        r(map(el->m(el[n]), nt))
    end)
end

```
