2

有人可以解释并提供with在定义类型时使用关键字的真实示例吗?

让我们定义类型

type T = A with B

这是什么意思?什么时候应该使用它?如何实例化类型T

4

1 回答 1

9

它被称为类型连接。

您可以使用它来强制某个类型必须扩展所有指定的特征/类。一个愚蠢的例子:

scala> trait Quackable {
     |   def quack = println("quack")
     | }
defined trait Quackable

scala> trait Walkable {
     |   def walk = println("walk")
     | }
defined trait Walkable

scala> case class Duck(name: String) extends Quackable with Walkable
defined class Duck

scala> def foo(d: Quackable with Walkable): Unit = {
     |   d.quack
     |   d.walk
     | }
foo: (d: Quackable with Walkable)Unit

scala> foo(Duck(""))
quack
walk

// Or you can create a type alias and use it.    
scala> type QW = Quackable with Walkable
defined type alias QW

scala> def foo(d: QW): Unit = {
     |   d.quack
     |   d.walk
     | }
foo: (d: QW)Unit

scala> foo(Duck(""))
quack
walk

// If you need to retain the type information for some reason, you can use a type parameter.

scala> def foo[A <: Quackable with Walkable](d: A): A = {
     |   d.quack
     |   d.walk
     |   d
     | }
foo: [A <: Quackable with Walkable](d: A)A

scala> foo(Duck(""))
quack
walk
res1: Duck = Duck()

至于“如何实例化它”:不要那样想。type创建类型别名/同义词/函数,它们不一定代表具体的可实例化类型。

编辑:

如果您熟悉 Java,with上面使用的类似于 Java 的&.

public static <QW extends Quackable & Walkable> void foo(QW d) {
  d.quack();
  d.walk();
}

然而,与 Java 不同的是&with它为您提供了正确的类型。我写的第一个定义foo不能翻译成Java。您也不能对 Java 的&.

scala> case object Quackawalkasaurus extends Quackable with Walkable
defined module Quackawalkasaurus

scala> List(Duck(""), Quackawalkasaurus)
res2: List[Product with Serializable with Quackable with Walkable] = List(Duck(), Quackawalkasaurus)

// Add an explicit type annotation if you want to remove unwanted common super-traits/classes.
scala> List(Duck(""), Quackawalkasaurus) : List[Quackable with Walkable]
res3: List[Quackable with Walkable] = List(Duck(), Quackawalkasaurus)
于 2012-10-23T11:55:06.247 回答