# Parametric type of parametric type

**URL:** <https://discourse.julialang.org/t/parametric-type-of-parametric-type/18168>\
**Category:** New to Julia\
**Tags:** parametric-types\
**Created:** [November 30, 2018, 2:09am UTC](https://discourse.julialang.org/t/parametric-type-of-parametric-type/18168 "2018-11-30T02:09:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [November 30, 2018, 2:09am UTC](https://discourse.julialang.org/t/parametric-type-of-parametric-type/18168/1 "2018-11-30T02:09:35Z")

</div>

hi,

how could I define a parametric type of a parametric type, like this:

```julia
julia> struct AA{T <: AbstractMatrix{S <: Number} }
           data::T
       end
ERROR: UndefVarError: S not defined
Stacktrace:
 [1] top-level scope at none:0

```

thanks.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [November 30, 2018, 2:24am UTC](https://discourse.julialang.org/t/parametric-type-of-parametric-type/18168/2 "2018-11-30T02:24:34Z")

</div>

You need S to be a parameter of the type as well:

```julia
struct AA{S, T <: AbstractMatrix{S}}
    ...

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 30, 2018, 8:05am UTC](https://discourse.julialang.org/t/parametric-type-of-parametric-type/18168/3 "2018-11-30T08:05:38Z")

</div>

You can also use a UnionAll constraint:

```julia
julia> struct AA{T <: AbstractMatrix{<:Number} }
                  data::T
              end

```

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [November 30, 2018, 10:28am UTC](https://discourse.julialang.org/t/parametric-type-of-parametric-type/18168/4 "2018-11-30T10:28:16Z")

</div>

how do you call it as a “UnionAll” constraint?

`struct AA{T <: AbstractMatrix{<:Number} }`

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 30, 2018, 11:19am UTC](https://discourse.julialang.org/t/parametric-type-of-parametric-type/18168/5 "2018-11-30T11:19:14Z")

</div>

I’m not quite sure what you mean. My suggestion is syntactic sugar for

```julia
julia> struct AA{T <: AbstractMatrix{T} where T<:Number }
                         data::T
       end

```

and the constructor just works:

```julia
julia> AA([1 2;1 2])
AA{Array{Int64,2}}([1 2; 1 2])

```

The trick is also useful for method defintions:

```julia
f(x::Matrix{<:Number}) = ...

```

instead of

```julia
f(x::Matrix{T} where T<:Number) = ...

```

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [December 5, 2018, 8:05am UTC](https://discourse.julialang.org/t/parametric-type-of-parametric-type/18168/6 "2018-12-05T08:05:13Z")

</div>

> [@mauro3](#):
>
> struct AA{T \<: AbstractMatrix{T} where T\<:Number } data::T end

maybe it’s more clear to write as the following?

```julia
struct BB{S <: AbstractMatrix{T} where T<:Number }
    data::S
end

```

---

<div class="post-metadata">

**Author:** ![mbesancon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbesancon/32/6528_2.png) [@mbesancon](https://discourse.julialang.org/u/mbesancon)\
**Post date:** [December 5, 2018, 8:39am UTC](https://discourse.julialang.org/t/parametric-type-of-parametric-type/18168/7 "2018-12-05T08:39:07Z")

</div>

If you don’t need the inner type `T<:Number` within the definition of the `struct`, what @mauro3 suggests is more concise
