Scala3 has support for "kind polymorphism". Docs also mention AnyKind
type:
AnyKind plays a special role in Scala's subtype system: It is a supertype of all other types no matter what their kind is.
Question:
- can anyone give a working code example how
AnyKind
generality is useful?
(surprisingly can't find any useful examples so far)
For example the type member
MirroredType
ofscala.deriving.Mirror.Product
/Mirror.Sum
is actually poly-kinded (although this is not written in the definition ofMirror
/Mirror.Sum
/Mirror.Product
)https://docs.scala-lang.org/scala3/reference/contextual/derivation.html#mirror
The type member
MirroredMonoType
has always kind*
, including being existential (A[?]
). ButMirroredType
can be*
or
* => *
etc.
Notice that
MirroredElemTypes
is also poly-kinded (MirroredElemTypes = (B, C)
,MirroredElemTypes[T] = (B, C)
, ...)So if I wanted to do something further with a tuple
MirroredElemTypes
then the only option would be to have upper boundAnyKind
Another example is
scala.quoted.Type
(thanks to @Max for pointing this out)https://contributors.scala-lang.org/t/proposal-to-add-kind-polymorphism-to-the-language/2958/16
Miles Sabin. Adding kind-polymorphism to the Scala programming language https://www.youtube.com/watch?v=v6e7rYOXdcM