Ask Your Question
0

Can coercive subtyping be honored by covariant/contravariant type constructors in scala 3?

asked 2023-07-17 21:16:16 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-07-17 21:30:02 +0000

devzero gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-17 21:16:16 +0000

Seen: 11 times

Last updated: Jul 17 '23