One can say a type parameter T must have a specific supertype S_1:
class Test[T <: S_1]
Is there a way to say, that a type parameter must have at least one supertype of multiple supertype alternatives ?
Something like (pseudocode) :
class Test[T <: S_1 || S_2]
Or: Is this not possible, because such a construction makes no sense and would be a hint of a design mistake in the code ?
Short answer: The intuitive solution is to make
S_1andS_2share a common trait that represents the set of abilities you require for your type parameterT. Use that trait as the upper bound forT.More possibilities:
If
S_1andS_2are unrelated in nature and your requirement for the typeTis that it has certain members (that bothS_1andS_2happen to implement), you can use a structural type to formulate that (the concept behind is called duck typing).If for some reason you really require
Tto be a subclass ofS_1orS_2, and you can’t change those types, you can use implicits to convert both of these to a newly introduced internal typeS_1_or_2, which you can then use as an upper bound for yourT.