# NamedTuple with explicit (abstract) types

**URL:** https://discourse.julialang.org/t/namedtuple-with-explicit-abstract-types/65000
**Category:** General Usage
**Tags:** question
**Created:** [July 20, 2021, 5:59pm UTC](https://discourse.julialang.org/t/namedtuple-with-explicit-abstract-types/65000 "2021-07-20T17:59:26Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Uroc327](https://avatars.discourse-cdn.com/v4/letter/u/eb8c5e/32.png) [@Uroc327](https://discourse.julialang.org/u/Uroc327)
#### Post date: [July 20, 2021, 5:59pm UTC](https://discourse.julialang.org/t/namedtuple-with-explicit-abstract-types/65000/1 "2021-07-20T17:59:26Z")

</div>

How can I create a `NamedTuple` with an abstract type?

When I try the following code, `nt.a` is of concrete type `Vector{Float64}` and the assignment `nt.a[] = arr` fails because a `Matrix{Int64}` cannot be converted to a `Vector{Float64}`.

```julia
arr = zeros(Int, 2, 3)

nt = (a = Ref(Array{Float64, 1}()), b = 3) # I'd like to store AbstractArray instead of Vector{Float64}
nt.a[] = arr # MethodError: no method matching Vector{Float64}(::Matrix{Int64})
nt.a[][2, 2] = 100

arr[2, 2] == 100

```

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [July 20, 2021, 6:06pm UTC](https://discourse.julialang.org/t/namedtuple-with-explicit-abstract-types/65000/2 "2021-07-20T18:06:07Z")

</div>

Just specify a looser type parameter (don’t worry about the `NamedTuple` part, also)

```julia
julia> x = Ref{AbstractArray}();

julia> x
Base.RefValue{AbstractArray}(#undef)

julia> x[] = [1];

julia> x[] = [1 2];

```

---

<div class="post-metadata">

### Author: ![Uroc327](https://avatars.discourse-cdn.com/v4/letter/u/eb8c5e/32.png) [@Uroc327](https://discourse.julialang.org/u/Uroc327)
#### Post date: [July 20, 2021, 6:15pm UTC](https://discourse.julialang.org/t/namedtuple-with-explicit-abstract-types/65000/3 "2021-07-20T18:15:36Z")

</div>

Thanks a lot!

I require the named tuple anyways, as I bundle some of those arrays.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [July 20, 2021, 7:04pm UTC](https://discourse.julialang.org/t/namedtuple-with-explicit-abstract-types/65000/4 "2021-07-20T19:04:41Z")

</div>

> [@Uroc327](#):
>
> How can I create a `NamedTuple` with an abstract type?

```julia
julia> x = @NamedTuple{a::Integer, b::AbstractArray}((3, [4,5]))
NamedTuple{(:a, :b), Tuple{Integer, AbstractArray}}((3, [4, 5]))

```
