# Type constraint in struct definition

**URL:** <https://discourse.julialang.org/t/type-constraint-in-struct-definition/90328>\
**Category:** General Usage\
**Tags:** parametric-types\
**Created:** [November 16, 2022, 6:29am UTC](https://discourse.julialang.org/t/type-constraint-in-struct-definition/90328 "2022-11-16T06:29:00Z")\
**Posts on this page:** 1\
**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:** [November 16, 2022, 6:29am UTC](https://discourse.julialang.org/t/type-constraint-in-struct-definition/90328/1 "2022-11-16T06:29:00Z")

</div>

I want to do something like the following:

```julia
abstract type Foo{V, S} end

# Does not work but the idea is that I want to constrain T and 
# use whatever U that makes the constraint in the 
# parameterization of the super type
struct Bar{T <: Foo{U}} <: Foo{U, Nothing} where U <: Real end

```

Right now the closest I can get in a way that works is

```julia
# Here V should be whatever <:Real actually comes out to
struct Bar{T <: Foo{<:Real}, V} <: Foo{V, Nothing} end

```

but this is not optimal since types like:

```julia
Bar{Foo{Int64, Int64}, Float64}

```

would be allowed (I want this to be an error since `Float64` should be `Int64` by the type constraint that I desire.

I understand that I can create an outer constructor

```julia
Bar(::Type{T}) where T <: Foo{V, S} where {V, S} = Bar{T, V}()

```

to alleviate that issue, but defining such a constructor is error prone and takes extra effort. Especially since I plan on defining hundreds of such subtypes.
