# Parametric type which contains the abstract type it subtypes

**URL:** <https://discourse.julialang.org/t/parametric-type-which-contains-the-abstract-type-it-subtypes/97774>\
**Category:** General Usage\
**Tags:** question, parametric-types\
**Created:** [April 22, 2023, 4:39am UTC](https://discourse.julialang.org/t/parametric-type-which-contains-the-abstract-type-it-subtypes/97774 "2023-04-22T04:39:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nharr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nharr/32/24698_2.png) [@nharr](https://discourse.julialang.org/u/nharr)\
**Post date:** [April 22, 2023, 4:39am UTC](https://discourse.julialang.org/t/parametric-type-which-contains-the-abstract-type-it-subtypes/97774/1 "2023-04-22T04:39:36Z")

</div>

I’m creating a type that is a matrix with value bounds.

This works:

```julia
struct Map{M<:AbstractMatrix{<:Real}}
    data::M
    lb
    ub
end

```

But I want it to behave like a matrix itself too, so a subtype.

This works:

```julia
struct Map{T} <: AbstractMatrix{T}
    data::Matrix{T}
    lb
    ub
end

```

But this specifies a concrete Matrix inside, which can’t change even if a different form of matrix is passed in. So I’ve tried many variations of `where T` but they all fail with “T not defined”:

```julia
struct Map{M<:AbstractMatrix{T}} <: AbstractMatrix{T} where T<:Real
    data::M
    lb
    ub
end

```

Is there a way to allow the new type to adapt to any subtype of AbstractMatrix while still subtyping AbstractMatrix? Or is my thought to preserve concrete-type flexibility wrong? I’m trying to get this functionality while avoiding the ambiguity problem discussed in [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-fields-with-abstract-containers).

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [April 22, 2023, 5:25am UTC](https://discourse.julialang.org/t/parametric-type-which-contains-the-abstract-type-it-subtypes/97774/2 "2023-04-22T05:25:09Z")

</div>

```julia
struct Map{T<:Real, M<:AbstractMatrix{T}} <: AbstractMatrix{T}
    data::M
    lb
    ub
end

```

You might want to give `lb` and `ub` a type as well, to avoid a type instability. The default for non-annotated fields in structs is `Any` after all.

Additionally, this will be helpful:

[![](https://global.discourse-cdn.com/julialang/original/3X/0/6/06b71d551368fc5bd6fdf3b9541952ba9c96799b.jpeg "An introduction to high performance custom arrays | Matt Bauman") ](https://www.youtube.com/watch?v=jS9eouMJf_Y)

---

<div class="post-metadata">

**Author:** ![nharr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nharr/32/24698_2.png) [@nharr](https://discourse.julialang.org/u/nharr)\
**Post date:** [April 22, 2023, 5:36am UTC](https://discourse.julialang.org/t/parametric-type-which-contains-the-abstract-type-it-subtypes/97774/3 "2023-04-22T05:36:46Z")

</div>

Perfect! Thanks for your help. So parameters within other parameters need to also be listed as parameters first?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [April 22, 2023, 6:08am UTC](https://discourse.julialang.org/t/parametric-type-which-contains-the-abstract-type-it-subtypes/97774/4 "2023-04-22T06:08:03Z")

</div>

Yes, if you want to use those parameters in your struct as well or otherwise want to introduce a cross-cutting constraint, like the one spanning both the declared supertype and a field (unfortunately).
