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.
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
Asked: 2021-08-08 11:00:00 +0000
Seen: 9 times
Last updated: Apr 16 '21
In SCSS, what is the method for grouping and reusing a set of classes and styles?
What is the method to distinguish the presence of a json field in an array using presto?
What is Nextflow for genomics in AWS?
What are the differences between TREEFROG, CROW, and the CPPCMS C++ framework?
What does "waiting for handler commit" mean in relation to the slow writes experienced in MySQL 8?
What is the best way to arrange the file structure for both the backend and frontend in MERN?