你遇到了一个常见的问题:
SI-2712。为了更加清晰,我会将你的代码简化一下:
import language.higherKinds
object Test {
case class Base[A](a: A)
case class Recursive[F[_], A](fa: F[A])
def main(args: Array[String]): Unit = {
val one = Base(1)
val two = Recursive(one)
val three = Recursive(two)
println(three)
}
}
这个示例展示了与你遇到的相同类型的错误:
argument expression's type is not compatible with formal parameter type;
found : Test.Recursive[Test.Base,Int]
required: ?F
val three = Recursive(two) // doesn't compile
^
首先,让我们了解一些您可能已经知道的语法和术语:
- 在Scala中,我们说一个普通的、未参数化的数据类型(例如
Int
)具有_
类型。它是单态的。
Base
,另一方面,是参数化的。如果不提供它包含的类型,我们不能将其用作值的类型,因此我们说它具有_[_]
类型。它是rank-1多态:一个接受类型的类型构造函数。
Recursive
更进一步:它有两个参数,F[_]
和A
。这里的类型参数数量并不重要,但它们的种类很重要。F[_]
是rank-1多态的,所以Recursive
是rank-2多态:它是一个接受类型构造函数的类型构造函数。
- 我们称任何rank 2或以上的东西为higher-kinded,这就是乐趣开始的地方。
总体而言,Scala对于higher-kinded类型没有问题。这是区别于Java等语言的几个关键特性之一。但是,在处理higher-kinded类型时,它确实遇到了部分应用类型参数的问题。
问题在于:Recursive[F[_], A]
有两个类型参数。在您的示例代码中,您使用了“类型lambda”技巧来部分应用第一个参数,类似于:
val one = Base(1)
val two = Recursive(one)
val three = {
type λ[α] = Recursive[Base, α]
Recursive(two : λ[Int])
}
这会让编译器确信你提供了与Recursive
构造函数中的正确类型(_[_]
)相符的内容。如果Scala支持柯里化的类型参数列表,我肯定会在这里使用:
case class Base[A](a: A)
case class Recursive[F[_]][A](fa: F[A])
def main(args: Array[String]): Unit = {
val one = Base(1)
val two = Recursive(one)
val three = Recursive(two)
println(three)
}
遗憾的是,这个问题没有解决(见SI-4719)。所以,据我所知,处理这个问题最常见的方法是使用“unapply trick”,这是由Miles Sabin提出的。下面是scalaz中一个大大简化过的版本:
import language.higherKinds
trait Unapply[FA] {
type F[_]
type A
def apply(fa: FA): F[A]
}
object Unapply {
implicit def unapply[F0[_[_], _], G0[_], A0] = new Unapply[F0[G0, A0]] {
type F[α] = F0[G0, α]
type A = A0
def apply(fa: F0[G0, A0]): F[A] = fa
}
}
在有些模糊的术语中,这个
Unapply
结构就像是一个“一流类型lambda”。我们定义了一个trait来表示某种类型
FA
可以分解为类型构造器
F[_]
和类型
A
的断言。然后在其伴随对象中,我们可以定义隐式转换来提供各种类型的具体分解。我只在这里定义了我们需要使得
Recursive
适用的特定分解,但你也可以编写其他分解。
有了这个额外的管道,我们现在可以做我们需要的事情:
import language.higherKinds
object Test {
case class Base[A](a: A)
case class Recursive[F[_], A](fa: F[A])
object Recursive {
def apply[FA](fa: FA)(implicit u: Unapply[FA]) = new Recursive(u(fa))
}
def main(args: Array[String]): Unit = {
val one = Base(1)
val two = Recursive(one)
val three = Recursive(two)
println(three)
}
}
Ta-da!现在类型推断已经可用,这段代码可以编译。作为练习,我建议您创建一个额外的类:
case class RecursiveFlipped[A, F[_]](fa: F[A])
......这与Recursive
在任何实质性方面都没有区别,但会再次破坏类型推断。然后定义所需的额外管道以修复它。祝你好运!
编辑
您要求一个更加关注类型类的版本,需要进行一些修改,但希望您能看到相似之处。首先,这是我们升级后的Unapply
:
import language.higherKinds
trait Unapply[TC[_[_]], FA] {
type F[_]
type A
def TC: TC[F]
def apply(fa: FA): F[A]
}
object Unapply {
implicit def unapply[TC[_[_]], F0[_[_], _], G0[_], A0](implicit TC0: TC[({ type λ[α] = F0[G0, α] })#λ]) =
new Unapply[TC, F0[G0, A0]] {
type F[α] = F0[G0, α]
type A = A0
def TC = TC0
def apply(fa: F0[G0, A0]): F[A] = fa
}
}
再次强调,这个方法完全借鉴自scalaz。下面是一些使用它的示例代码:
import language.{ implicitConversions, higherKinds }
object Test {
trait Functor[F[_]] {
def map[A, B](fa: F[A])(f: A => B): F[B]
}
object Functor {
implicit class FunctorOps[F[_], A](fa: F[A])(implicit F: Functor[F]) {
def map[B](f: A => B) = F.map(fa)(f)
}
implicit def unapply[FA](fa: FA)(implicit u: Unapply[Functor, FA]) =
new FunctorOps(u(fa))(u.TC)
}
case class Id[A](value: A)
object Id {
implicit val idFunctor = new Functor[Id] {
def map[A, B](fa: Id[A])(f: A => B) = Id(f(fa.value))
}
}
case class Pair[F[_], A](lhs: F[A], rhs: F[A])
object Pair {
implicit def pairFunctor[F[_]](implicit F: Functor[F]) = new Functor[({ type λ[α] = Pair[F, α] })#λ] {
def map[A, B](fa: Pair[F, A])(f: A => B) = Pair(F.map(fa.lhs)(f), F.map(fa.rhs)(f))
}
}
def main(args: Array[String]): Unit = {
import Functor._
val one = Id(1)
val two = Pair(one, one) map { _ + 1 }
val three = Pair(two, two) map { _ + 1 }
println(three)
}
}