# Parametric supertyping using struct field parameters

**URL:** <https://discourse.julialang.org/t/parametric-supertyping-using-struct-field-parameters/81920>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 30, 2022, 3:06pm UTC](https://discourse.julialang.org/t/parametric-supertyping-using-struct-field-parameters/81920 "2022-05-30T15:06:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)\
**Post date:** [May 30, 2022, 3:06pm UTC](https://discourse.julialang.org/t/parametric-supertyping-using-struct-field-parameters/81920/1 "2022-05-30T15:06:31Z")

</div>

Hello,

I’m trying to play with supertypes but I’m encountering an issue so I made this MWE:

```julia
abstract type AbstractFoo{T} end

struct Foo{T} <: AbstractFoo{T}
    x::T
end

```

So far so good. Now I would like to define a wrapper of `Foo` that has the same parametric supertype as its wrapped object. That means I need access to the parametres of the type of the wrapped foo. Ideally I would be able to do this:

```julia
struct Bar{F <: AbstractFoo} <: supertype(F)
    foo::F
end

```

But Julia does not allow this kind of stuff I recon. So I tried to access `T` directly like so

```julia
struct Bar{F <: AbstractFoo{T}} <: AbstractFoo{T} 
    foo::F
end

```

which tells me that `T` is not defined. How do I define it? Note that I use `F <: AbstractFoo{T}` because I would like `foo` to be any `<: AbstractFoo` even though I defined only one here.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [May 30, 2022, 3:49pm UTC](https://discourse.julialang.org/t/parametric-supertyping-using-struct-field-parameters/81920/2 "2022-05-30T15:49:33Z")

</div>

```julia
struct Bar{T, F <: AbstractFoo{T}} <: AbstractFoo{T} 
    foo::F
end

```

Should work

---

<div class="post-metadata">

**Author:** ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)\
**Post date:** [May 30, 2022, 3:59pm UTC](https://discourse.julialang.org/t/parametric-supertyping-using-struct-field-parameters/81920/3 "2022-05-30T15:59:00Z")

</div>

It does thanks!

So it’s mandatory to have the `T` redundantly appear twice in the `Bar` parameters?

---

<div class="post-metadata">

**Author:** ![tsela](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tsela/32/52174_2.png) [@tsela](https://discourse.julialang.org/u/tsela)\
**Post date:** [May 30, 2022, 5:59pm UTC](https://discourse.julialang.org/t/parametric-supertyping-using-struct-field-parameters/81920/4 "2022-05-30T17:59:16Z")

</div>

It’s not redundant. Your `Bar` struct depends on two type parameters, so those need to be defined as such. And you can’t have a type parameter definition on the right of a `<:` symbol. You can at best _use_ a type parameter there that has been defined earlier.
