返回首页 Scala 课堂

Scala 课堂:高级类型(二)

这里我们转载 Twitter 的 Scala 课堂 ,转载的内容基本来自 Twitter 的 Scala 课堂中文翻译,部分有小改动.

更高级多态性类型 和 特设多态性

Scala 可以对“更高阶”的类型进行抽象。例如,假设您需要用几种类型的容器处理几种类型的数据。你可能定义了一个 Container 的接口,它可以被实现为几种类型的容器:Option、List 等。你要定义可以使用这些容器里的值的接口,但不想确定值的类型。 这类似与函数柯里化。例如,尽管“一元类型”有类似 List[A] 的构造函数,这意味着我们必须满足一个“级别”的类型变量来产生一个具体的类型(就像一个没有柯里化的函数需要只提供一个参数列表来被调用),更高阶的类型需要更多。


scala>  trait Container[M[_]] { def put[A](x: A): M[A]; def get[A](m: M[A]): A }
defined trait Container

scala>  val container = new Container[List] { def put[A](x: A) = List(x); def get[A](m: List[A]) = m.head }
container: Container[List] = $anon$1@434013d9

scala>  container.put("hey")
res1: List[String] = List(hey)

scala>  container.put(123)
res2: List[Int] = List(123)

注意: Container 是参数化类型的多态(“容器类型”)。 如果我们结合隐式转换 implicits 使用容器,我们会得到“特设的”多态性:即对容器写泛型函数的能力。


scala>  trait Container[M[_]] { def put[A](x: A): M[A]; def get[A](m: M[A]): A }
defined trait Container

scala> implicit val listContainer = new Container[List] { def put[A](x: A) = List(x); def get[A](m: List[A]) = m.head }
listContainer: Container[List] = $anon$1@54f3d86c

scala> implicit val optionContainer = new Container[Some] { def put[A](x: A) = Some(x); def get[A](m: Some[A]) = m.get }
optionContainer: Container[Some] = $anon$1@591287f8

scala> :paste
// Entering paste mode (ctrl-D to finish)

def tupleize[M[_]: Container, A, B](fst: M[A], snd: M[B]) = {
      val c = implicitly[Container[M]]                             
      c.put(c.get(fst), c.get(snd))
      }

// Exiting paste mode, now interpreting.

tupleize: [M[_], A, B](fst: M[A], snd: M[B])(implicit evidence$1: Container[M])M[(A, B)]

scala>  tupleize(Some(1), Some(2))
res0: Some[(Int, Int)] = Some((1,2))

scala>  tupleize(List(1), List(2))
res1: List[(Int, Int)] = List((1,2))

F-界多态性

通常有必要来访问一个(泛型)特质的具体子类。例如,想象你有一些泛型特质,但需要可以与它的某一子类进行比较。


trait Container extends Ordered[Container]

然而,现在比较方法是必须的了


def compare(that: Container): Int

因此,我们不能访问具体子类型,例如


class MyContainer extends Container {
  def compare(that: MyContainer): Int
}

编译失败,因为我们对 Container 指定了 Ordered 特质,而不是对特定子类型指定的。 为了调和这一点,我们改用 F-界的多态性。


trait Container[A <: Container[A]] extends Ordered[A]

奇怪的类型!但可以看到怎样对 A 实现了 Ordered 参数化,它本身就是 Container[A]


class MyContainer extends Container[MyContainer] { 
  def compare(that: MyContainer) = 0 
}

他们是有序的了:


scala>  List(new MyContainer, new MyContainer, new MyContainer)
res0: List[MyContainer] = List(MyContainer@50e205df, MyContainer@26ef9cf5, MyContainer@3d29accb)

scala>  List(new MyContainer, new MyContainer, new MyContainer).min
res1: MyContainer = MyContainer@622e8c17

鉴于他们都是 Container[] 的子类型,我们可以定义另一个子类并创建 Container[] 的一个混合列表:


scala> class YourContainer extends Container[YourContainer] { def compare(that: YourContainer) = 0 }
defined class YourContainer

scala> List(new MyContainer, new MyContainer, new MyContainer, new YourContainer)               
res2: List[Container[_ >: YourContainer with MyContainer <: Container[_ >: YourContainer with MyContainer <: Object]]] = List(MyContainer@58f59add, MyContainer@648a50cb, MyContainer@34be72fe, YourContainer@436f9cbf)

注意结果类型是怎样成为 YourContainer 和 MyContainer 类型确定的下界。这是类型推断的工作。有趣的是,这种类型甚至不需要是有意义的,它只是提供了一个合乎逻辑的最大下界为列表的统一类型。如果现在我们尝试使用 Ordered 会发生什么?


scala> (new MyContainer, new MyContainer, new MyContainer, new YourContainer).min
<console>:11: error: value min is not a member of (MyContainer, MyContainer, MyContainer, YourContainer)
              (new MyContainer, new MyContainer, new MyContainer, new YourContainer).min

对统一的类型 Ordered[]不存在了。太糟糕了。

 结构类型

Scala 支持 结构类型 structural types — 类型需求由接口 构造 表示,而不是由具体的类型表示。


scala>  def foo(x: { def get: Int }) = 123 + x.get
foo: (x: AnyRef{def get: Int})Int

scala>  foo(new { def get = 10 })                 
res4: Int = 133

 抽象类型成员

在特质中,你可以让类型成员保持抽象。


scala>  trait Foo { type A; val x: A; def getX: A = x }
defined trait Foo

scala> (new Foo { type A = Int; val x = 123 }).getX   
res5: Int = 123

scala>  (new Foo { type A = String; val x = "hey" }).getX
res6: String = hey

在做依赖注入等情况下,这往往是一个有用的技巧。

您可以使用 hash 操作符来引用一个抽象类型的变量:


scala> trait Foo[M[_]] { type t[A] = M[A] }
defined trait Foo

scala> val x: Foo[List]#t[Int] = List(1)
x: List[Int] = List(1)

 类型擦除和清单

正如我们所知道的,类型信息在编译的时候会因为 擦除 而丢失。 Scala 的 清单(Manifests) 功能,使我们能够选择性地恢复类型信息。清单提供了一个隐含值,根据需要由编译器生成。


scala>  class MakeFoo[A](implicit manifest: Manifest[A]) { def make: A = manifest.erasure.newInstance.asInstanceOf[A] }
defined class MakeFoo

scala> (new MakeFoo[String]).make
res7: String = ""