Ask Your Question
0

Is it possible to extract a higher kind argument in Scala 2 or 3 without using match type?

asked 2021-08-08 11:00:00 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-04-16 12:00:00 +0000

lalupa gravatar image

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.

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: 2021-08-08 11:00:00 +0000

Seen: 9 times

Last updated: Apr 16 '21