Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Yes, Scala 3 has built-in support for coercive subtyping through the use of variance annotations on type constructors. A type constructor can be marked as covariant (+), contravariant (-), or invariant (no annotation) to indicate how it behaves under subtyping.

Covariant type constructors can be used when the type parameter is used only in covariant positions. For example, the type constructor List[+A] is covariant in A because a list of subtypes is always a list of supertypes.

Contravariant type constructors can be used when the type parameter is used only in contravariant positions. For example, the type constructor Function1[-A, +B] is contravariant in A because a function that accepts a supertype can be passed a subtype, but it's covariant in B because a function that produces a subtype can be treated as producing a supertype.

In Scala 3, variance annotations on type constructors are checked at compile-time and enforced at runtime, ensuring that coercive subtyping is accounted for in the type system.