Having following definition
type MyMap = Map[String, List[Map[Int, String]]]
Can Map be defined as a higher kinded type ?
"Kind" is a word used to classify types and type constructors according to their level of abstractness. Concrete, fully specified types such as `Int` and `Option[Int]` are called "proper types" and denoted as `A` using Scala notation, or with the `*` symbol.
One use case of higher-kinded types is in creating polymorphic containers. Higher-kinded types are useful when we want to create a container that can hold any type of items; we don’t need a different type for each specific content type. As we already saw, Collection (in our previous example) allows various entity types.
Scalaz, one of the most popular Scala projects, uses higher-kinded types to extend the core Scala library for functional programming. 4.2. Polymorphic Containers
Scala type systems contain variances like invariant, covariance and contravariance but the higher kinded type is different from them. We can either create custom type classes in Scala or we can use some of the existing Scala type classes like List [T], Option [T] and more.
What you have is not a higher-kinded type, but it could be quite easily modified to be such.
type MyMap2[A,B,C] = Map[A, List[Map[B, C]]]
Now, we can create MyMap
again by providing type parameters.
type MyMap = MyMap2[String, Int, String]
"Higher-kinded" just means that it is a type that is uninhabited, and needs to be provided with other types in order to create an inhabitable type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With