【发布时间】:2018-02-23 14:50:23
【问题描述】:
我正在向 scala-exercises 学习猫。
想知道如何使用高阶类型,有一些尝试:
trait Functor[F[_]] {
def map[A, B](fa: F[A])(f: A => B): F[B]
}
def someThingFail1[In]() = new Functor[Function1[In, _]] {
override def map[A, B](fa: Function1[In, A])(f: A => B): Function1[In, B] = ???
}
def someThingFail2[In]() = new Functor[Either[In, _]] {
override def map[A, B](fa: Either[In, A])(f: A => B): Either[In, B] = ???
}
def someThingFail3() = new Functor[List[_]] {
override def map[A, B](fa: List[A])(f: A => B): List[B] = ???
}
//only this one can compile
def someThingRight1() = new Functor[List] {
override def map[A, B](fa: List[A])(f: A => B): List[B] = ???
}
前面三个函数不能编译,错误信息如下:
[error] /Users/lorancechen/version_control_project/_tutorials/learn-cats/src/main/scala/mycats/Main.scala:16:42: Either[In, _] takes no type parameters, expected: one
[error] def someThingFail2[In]() = new Functor[Either[In, _]] {
[error] ^
为什么 Scala 不支持类型孔? Dotty 编译器会支持它吗?谢谢
【问题讨论】:
标签: scala scala-cats type-constructor