斯卡拉SIP-20提出了一个新的lazy val实现,它更正确,但比“当前”版本慢25%。
该建议的实施是这样的:
class LazyCellBase { // in a Java file - we need a public bitmap_0
  public static AtomicIntegerFieldUpdater<LazyCellBase> arfu_0 =
    AtomicIntegerFieldUpdater.newUpdater(LazyCellBase.class, "bitmap_0");
  public volatile int bitmap_0 = 0;
}
final class LazyCell extends LazyCellBase {
  import LazyCellBase._
  var value_0: Int = _
  @tailrec final def value(): Int = (arfu_0.get(this): @switch) match {
    case 0 =>
      if (arfu_0.compareAndSet(this, 0, 1)) {
        val result = 0
        value_0 = result
        @tailrec def complete(): Unit = (arfu_0.get(this): @switch) match {
          case 1 =>
            if (!arfu_0.compareAndSet(this, 1, 3)) complete()
          case 2 =>
            if (arfu_0.compareAndSet(this, 2, 3)) {
              synchronized { notifyAll() }
            } else complete()
        }
        complete()
        result
      } else value()
    case 1 =>
      arfu_0.compareAndSet(this, 1, 2)
      synchronized {
        while (arfu_0.get(this) != 3) wait()
      }
      value_0
    case 2 =>
      synchronized {
        while (arfu_0.get(this) != 3) wait()
      }
      value_0
    case 3 => value_0
  }
}
截至2013年6月,该SIP尚未获得批准。我希望根据邮件列表讨论,它可能会被批准并包含在Scala的未来版本中。因此,我认为您应该注意Daniel Spiewak的观察:
  懒惰的val *不是*免费的(甚至便宜)。仅当您绝对需要懒惰以获得正确性而不是优化时才使用它。
               
              
bitmap$0在当前实现中该字段是可变的(2.8)。