1 | initial version |
No, it is not possible to extract a higher kind argument in Scala 2 or 3 without using match type. Match type is the only way to pattern match on a type parameter that has a kind higher than * (i.e., a type constructor).
For example, consider the following code:
trait Functor[F[_]] {
def map[A, B](fa: F[A])(f: A => B): F[B]
}
To implement the map
method, we need to extract the type constructor F
from the type parameter F[_]
. In Scala 2 or 3, we can only do this using match type:
trait Functor[F[_]] {
def map[A, B](fa: F[A])(f: A => B): F[B] = fa match {
case fa: F[t] => ??? // Here we use match type to extract the type constructor F
}
}
Without match type, we would need to rely on the user of the Functor
trait to provide an implicit evidence of the Functor
instance for the specific type F
, which is less convenient and can lead to boilerplate code.