使用“私人[这]懒惰的val”是否有意义?

时间:2015-11-18 12:45:35

标签: scala

我理解为什么这样做很有用 private[this] val xx = 3
正如https://github.com/databricks/scala-style-guide#privatethis所述。

这同样适用于lazy val吗? Q1。是 private[this] lazy val xx = 3
比...快 private val xx = 3

Q2。 def f(i: Int) = i
private[this] lazy val xx = f(3)
比...快 private val xx = f(3)

2 个答案:

答案 0 :(得分:4)

没有。 具有private lazy val x= 3private[this] lazy val =3的类的生成类是相同的,解释如下。

<强> private lazy val

public class LazyValTest
{
  private int bar;
  private volatile boolean bitmap$0;

  private int bar$lzycompute()
  {
    synchronized (this) { if (!this.bitmap$0) { this.bar = 3; this.bitmap$0 = true; } return this.bar; }  } 
  private int bar() { return this.bitmap$0 ? this.bar : bar$lzycompute();
  }
}

使用私有和私有[this] val生成的类略有不同。

<强> private val

public class PrivateValTest
{
  private final int bar = 3;

  private int bar() { return this.bar; }
}

<强> private[this] val

public class PrivateThisValTest
{
  private final int bar = 3;
}

答案 1 :(得分:2)

不 - 懒惰的val总是被编码为带有支持字段的方法,所以private[this]或者the file /home/supreme/domains/wefact.supremehosting.nl/public_html/bestellen/application /wefact_functions.php requires the ionCube PHP Loader ioncube_loader_lin_5.5.so to be installed by the website operator. If you are the website operator please use the ionCube Loader Wizard to assist with installation. 如果你使值变得懒惰,你总是pay the cost of the accessor。 JIT编译器可能后来认识到它可以内联支持字段访问(在第一次加载之后),但您引用的样式指南似乎侧重于性能保证,并且这种内联似乎不能保证正确现在

相关问题