扩展 Luis 和 Andrey 的答案,使用 Scala 2 和 scala-cats 尝试使用类型别名
import cats.Functor
type MapInt[T] = Map[Int, T]
Functor[MapInt].map(Map(1 -> "woo"))(a => a + "hoo")
// : MapInt[String] = Map(1 -> "woohoo")
或使用 Scala 2 类型的 lambda "atrocity"
Functor[({type MapInt[T]=Map[Int, T]})#MapInt].map(Map(1 -> "woo"))(a => a + "hoo")
// : MapInt[String] = Map(1 -> "woohoo")
或使用kind-projector
Functor[Map[Int, *]].map(Map(1 -> "woo"))(a => a + "hoo")
// : MapInt[String] = Map(1 -> "woohoo")
scastie
关于 Map 对 Functor 的“种类”错误,请尝试使用 REPL 来探索这个想法(如果您以 sbt console 开头,它应该加载来自 build.sbt 的所有依赖项):
scala> import cats.Functor
import cats.Functor
scala> :kind -v Functor
cats.Functor's kind is X[F[A]]
(* -> *) -> *
This is a type constructor that takes type constructor(s): a higher-kinded type.
scala> :kind -v Map
Map's kind is F[A1,+A2]
* -> * -(+)-> *
This is a type constructor: a 1st-order-kinded type.
scala> type MapInt[T] = Map[Int, T]
type MapInt
scala> :kind -v MapInt
MapInt's kind is F[A]
* -> *
This is a type constructor: a 1st-order-kinded type.
注意Functor 有高阶种类
(* -> *) -> *
\______/
|
required shape of type argument to Functor
这意味着它需要一个一阶类型的类型构造函数,特别是一个接受单个类型参数的类型构造函数
* -> *
|
only one type argument expected
现在Map 类型构造函数本身确实具有Functor 所期望的一阶类型,但是它是错误的arity,因为它需要两个类型参数而不是一个
1st arg to Map
|
* -> * --> *
|
2nd arg to Map
因此,我们需要一个类型 lambda 来将 Map 的第一个类型参数固定为 Int,同时保持第二个类型参数空闲,这会将其转换为 Functor 的正确类型和数量的类型构造函数
scala> :kind -v MapInt
MapInt's kind is F[A]
* -> *
这是一个高阶类型构造函数的示例,它采用另一个二元一阶类型构造函数作为其类型参数
trait Foo[F[A, B]]
让我们检查 Map 现在是否适合而无需使用类型 lambda
scala> trait Foo[F[A, B]]
trait Foo
scala> :kind -v Foo
Foo's kind is X[F[A1,A2]]
(* -> * -> *) -> *
This is a type constructor that takes type constructor(s): a higher-kinded type.
scala> new Foo[Map] {}
val res2: Foo[Map] = $anon$1@589af27e