# Best practices to add more than one bound to type parameters

**URL:** <https://discourse.julialang.org/t/best-practices-to-add-more-than-one-bound-to-type-parameters/117212>\
**Category:** General Usage\
**Tags:** parametric-types, struct\
**Created:** [July 19, 2024, 5:41am UTC](https://discourse.julialang.org/t/best-practices-to-add-more-than-one-bound-to-type-parameters/117212 "2024-07-19T05:41:49Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![frankwswang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frankwswang/32/18561_2.png) [@frankwswang](https://discourse.julialang.org/u/frankwswang)\
**Post date:** [July 21, 2024, 9:48pm UTC](https://discourse.julialang.org/t/best-practices-to-add-more-than-one-bound-to-type-parameters/117212/21 "2024-07-21T21:48:10Z")

</div>

> [@frankwswang](#):
>
> `N` is not explicitly presented, which is what I really wanted. Because in practice, I want something like:
> 
> ```julia
> struct myT{T, N, V<:NTuple{N, T}}
> a::V # V should also be constrained to subtypes of TwoOrThreeTuple{T}
> b::Array{T, N}
> c::Tuple{Int, Vararg{V, N}}
> # more fields...
> end
> 
> ```

Please refer to the above demo. My end goal is to add more type fields with concrete type constraints while introducing **as few new type parameters as possible**.

**This includes adding more constraints to a single field, which is what my original simplified `MWE` was trying to show:**

> [@frankwswang](#):
>
> MWE:
> 
> ```julia
> julia> const TwoOrThreeTuple{T} = Union{NTuple{2, T}, NTuple{3, T}}
> Union{Tuple{T, T}, Tuple{T, T, T}} where T
> 
> julia> struct myT{T, N, NTuple{N, T}<:V<:TwoOrThreeTuple{T}}
> a::V
> end
> 
> julia> myT((1,2))
> ERROR: UndefVarError: `N` not defined
> 
> ```

In this way, I can (re-)use `N` and `T` to constrain more fields like `.b::Array{T, N}` and `.c::Tuple{Int, Vararg{V, N}}` without introducing new dedicated type parameters for them.

Additionally, by reusing the same type parameters, I hope to provide more information to the compiler for type inference when certain functions can only access part of the composite types. Although, this is not necessarily achievable given the current state of Julia’s compiler. That’s why I didn’t mention it before.

---

<div class="post-metadata">

**Author:** ![frankwswang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frankwswang/32/18561_2.png) [@frankwswang](https://discourse.julialang.org/u/frankwswang)\
**Post date:** [July 21, 2024, 9:52pm UTC](https://discourse.julialang.org/t/best-practices-to-add-more-than-one-bound-to-type-parameters/117212/22 "2024-07-21T21:52:20Z")

</div>

Ehm… I’ll think about it… Thanks!

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 21, 2024, 10:13pm UTC](https://discourse.julialang.org/t/best-practices-to-add-more-than-one-bound-to-type-parameters/117212/23 "2024-07-21T22:13:39Z")

</div>

This is definitely a more interesting example. Of course, you could even here reduce the number of type parameters to two, ditching the `V`, while fully expressing the type coupling, if you constrained `N` in the constructor instead.

It’s still not entirely clear to me why that simplification isn’t an option, but I understood that it’s not.

---

<div class="post-metadata">

**Author:** ![frankwswang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frankwswang/32/18561_2.png) [@frankwswang](https://discourse.julialang.org/u/frankwswang)\
**Post date:** [July 21, 2024, 10:18pm UTC](https://discourse.julialang.org/t/best-practices-to-add-more-than-one-bound-to-type-parameters/117212/24 "2024-07-21T22:18:45Z")

</div>

Yeah… I wish there were more ways for us to manipulate/control `TypeVar`s…

[Previous page](https://discourse.julialang.org/t/best-practices-to-add-more-than-one-bound-to-type-parameters/117212.md?page=1)
