Streams.scala中Cons单元实现中的通配符语法

时间:2013-10-25 20:57:11

标签: scala lazy-evaluation

/** A lazy cons cell, from which streams are built. */
  @SerialVersionUID(-602202424901551803L)
  final class Cons[+A](hd: A, tl: => Stream[A]) extends Stream[A] with Serializable {
    override def isEmpty = false
    override def head = hd
    @volatile private[this] var tlVal: Stream[A] = _
    def tailDefined: Boolean = tlVal ne null
    override def tail: Stream[A] = {
      if (!tailDefined)
        synchronized {
          if (!tailDefined) tlVal = tl
        }

      tlVal
    }
  }

_中的通配符@volatile private[this] var tlVal: Stream[A] = _代表什么?

1 个答案:

答案 0 :(得分:2)

通配符为var分配一个默认值,这使得变量初始化并可供使用;据我所知,从null开始的AnyRefAnyVal的'默认'值(0的数字{{1}对于false等)。

您不能声明已声明但未定义的局部变量。示例说明卷:

Boolean