Having following definition
type MyMap = Map[String, List[Map[Int, String]]]
Can Map be defined as a higher kinded type ?
Having following definition
type MyMap = Map[String, List[Map[Int, String]]]
Can Map be defined as a higher kinded type ?
What you have is not a higher-kinded type, but it could be quite easily modified to be such.
Now, we can create
MyMap
again by providing type parameters."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.
It should not.
You can make an analogy with values and functions. You have basic values, which are not functions, such as
5
and"foo"
. You have then simple functions, which takes simple values as arguments and return simple values, such as+
orlength
. Higher order functions are functions which have other functions as parameters or result. For instancetakeWhile
,map
, orfoldLeft
are higher order functions.If you consider types, there are simple types, which are the actual types of values , such as
Int
,String
, or evenInt => String
and List[Double] (now I consider every values, simple or not, including functions). Then there are parameteric types, which may also be called type constructors (calling them type functions would make the analogy clearer). List (without instanciating the generic parameter) is not really a type for values, you cannot declare a val to be just of typeList
, it has to beList[Something]
. So List may be considered as a function that given a simple type (say Int) returns another simple type (List[Int]).Int
,String
, andDouble
and Int => String are said to have kind*
, whileList
has kind* -> *
. Parametric types such asList
orMap
are the analogous of simple functions.Just as a higher order function is a function with function (instead of simple value) parameter, a Higher order type (or sometimes higher kinded) is a type that has a type constructor parameter, rather than just simple type parameters. It has kind
(* -> *) -> *
, or something more complex. They are declared withHigherOrder[C[_]]
, orHigherOrder[C[X]]
, to tell that the type parameter,C
, is itself a parametric type, or type constructor. Note that this has to appear in the type declaration, not the type instantiation.List
is declared traitList[A]
, so it is parametric, but not higher order. If you instanciate one withList[Seq[Map[Int, Set[Double]]]
, that won't makeList
's order any higher. A higher order type would acceptList
(rather thanList[Int]
) as its parameter, one could declareval x : HigherOrder[List] = ...
.Higher order types are not too common in the library, you can spot a few in the gory details of the collection library, such as GenericCompanion. You can find a lot of them in scalaz.