6

value classes 的规范中,它说:

值类只能扩展通用特征,不能扩展自身。通用特征是扩展的特征Any,只有defs 作为成员,并且不进行初始化。通用特征允许对值类的方法进行基本继承,但它们会产生分配的开销。例如

trait Printable extends Any {
  def print(): Unit = println(this)
}
class Wrapper(val underlying: Int) extends AnyVal with Printable

val w = new Wrapper(3)
w.print() // actually requires instantiating a Wrapper instance

第一个问题

现在,我认为这意味着以下(可能)不需要实例化:

trait Marker extends Any
class Wrapper(val underlying: Int) extends AnyVal with Marker {
  def print(): Unit = println(this) //unrelated to Marker
}

val w = new Wrapper(3)
w.print() //probably no instantiation as print is unrelated to Marker

我对么?

第二个问题

而且我认为这是否需要实例化是有机会的:

trait Printable extends Any {
  def print(): Unit //no implementation
}
class Wrapper(val underlying: Int) extends AnyVal with Printable {
  override def print() = println(this) //moved impl to value class
}

val w = new Wrapper(3)
w.print() // possibly requires instantiation

在概率的平衡上,我也认为不需要实例化——我正确吗?

编辑

我没有考虑print()示例中的确切实现:

def print(): Unit = println(this)

假设我使用了以下内容:

def print(): Unit = println(underlying)

这些会导致实例化吗?

4

1 回答 1

3

我对么?

不,如果我们发出最终的编译输出,我们可以看到它-Xprint:jvm

<synthetic> object F$Wrapper extends Object {
  final def print$extension($this: Int): Unit = 
    scala.Predef.println(new com.testing.F$Wrapper($this));

这是因为println有一个类型签名需要Any,所以我们在这里自责,因为我们实际上是“将值类 ttpe 视为另一种类型”。

虽然调用被调度到静态方法调用:

val w: Int = 3;
F$Wrapper.print$extension(w)

我们仍然在内部进行分配print$extension


如果我们偏离 using Wrapper.this,那么您的第一个假设确实是正确的,我们可以看到编译器愉快地展开Wrapper

<synthetic> object F$Wrapper extends Object {
  final def print$extension($this: Int): Unit = 
    scala.Predef.println(scala.Int.box($this));

呼叫站点现在看起来像这样:

val w: Int = 3;
com.testing.F$Wrapper.print$extension(w)

这对您的两个示例现在都有效,因为不需要在创建的接口上进行任何动态调度。

于 2017-11-06T16:25:26.017 回答