Given the following Scala definitions
abstract class C {
type T1 <: { def m() : Int }
type T2 <: { def n() : Int }
}
is there a way to define a third type within C that is constrained to be a subtype of both T1 and T2? E.g.
type T3 <: T1 & T2 // does not compile
It seems to me that (part of) the reason this won’t work as written is that I cannot be sure that this will not lead to an illegal constraint (e.g. inheriting from two classes). Thus, a related question would be if I can constrain T1 and T2 so that this would be legal, e.g. requiring that they both be traits.
Does this do what you need?
This doesn’t require
T1andT2to both be traits – you could make a valid implementation using one trait and a class, for example (it doesn’t matter which one is which).If you tried to define a concrete subtype of
CwhereT1andT2were both classes then it would not compile, so I would not worry about enforcing this in the constraint.