Best practices to add more than one bound to type parameters

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:

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.

Ehm… I’ll think about it… Thanks!

1 Like

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.

1 Like

Yeah… I wish there were more ways for us to manipulate/control TypeVars…