# How to restrict type parameter to be subclass of a parameterized type

**URL:** <https://discourse.julialang.org/t/how-to-restrict-type-parameter-to-be-subclass-of-a-parameterized-type/81854>\
**Category:** General Usage\
**Tags:** parametric-types\
**Created:** [May 29, 2022, 3:57am UTC](https://discourse.julialang.org/t/how-to-restrict-type-parameter-to-be-subclass-of-a-parameterized-type/81854 "2022-05-29T03:57:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![HashBrown](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hashbrown/32/43601_2.png) [@HashBrown](https://discourse.julialang.org/u/HashBrown)\
**Post date:** [May 29, 2022, 3:57am UTC](https://discourse.julialang.org/t/how-to-restrict-type-parameter-to-be-subclass-of-a-parameterized-type/81854/1 "2022-05-29T03:57:43Z")

</div>

I am looking to perform something like this:

```julia
abstract type Foo{T} end

struct Bar{T, U} <: Foo{U}
    bar :: U
end

struct Baz{T} <: Foo{U} where T <: Foo{U} where U
    baz :: U
end

```

That is I would like to parameterize a Baz by a Bar in a way that the baz property of the Baz has the same type as the bar property of the Bar. Both Baz and Bar should subtype the Foo that is parameterized by their field type.

I have tried a bunch of ways of trying to get the concept but they all throw an error. Namely below I show what I have tried

```julia
struct Baz{T} <: Foo{U} where T <: Foo{U} where U
    baz :: U
end

struct Baz{T} <: Foo{U} where T <: Foo{U}
    baz :: U
end

struct Baz{T where T <: Foo{U}} <: Foo{U}
    baz :: U
end

struct Baz{Foo{U}} <: Foo{U}
    baz :: U
end

```

What am I missing here? Is this possible?

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [May 29, 2022, 9:15am UTC](https://discourse.julialang.org/t/how-to-restrict-type-parameter-to-be-subclass-of-a-parameterized-type/81854/2 "2022-05-29T09:15:18Z")

</div>

```julia
# This part is the struct's where clause.
# U written first because T<:Foo{U} depends on U
# vvvvvvvvvvvvvv
struct Baz{U, T<:Foo{U}} <: Foo{U}
  baz :: U
end

```

`where {T<:Foo{U}} where U` is equivalent to `where {U, T<:Foo{U}}`, it’s just you need to write it like the latter because you can’t write `where`, let alone chains of them, in a `struct` definition’s where clause. You can write `where` in iterated union types annotating the fields, though.
