什么=> scala中没有右操作数的意思?

时间:2017-06-29 16:16:08

标签: scala

我知道=>操作员意味着E.g Int =>布尔值,但在Trait定义中,我没有看到任何正确的操作数。什么" self:Repr =>"意思?我只能部分填补它。自我是变种?接受输入Repr并返回什么的函数?

trait LinearSeqLike[+A, +Repr <: LinearSeqLike[A, Repr]] extends SeqLike[A, Repr] {
  self: Repr =>

  override protected[this] def thisCollection: LinearSeq[A] = this.asInstanceOf[LinearSeq[A]]
  override protected[this] def toCollection(repr: Repr): LinearSeq[A] = repr.asInstanceOf[LinearSeq[A]]

  def seq: LinearSeq[A]

  override def hashCode()= scala.util.hashing.MurmurHash3.seqHash(seq) // TODO - can we get faster via "linearSeqHash" ?

  override /*IterableLike*/
  def iterator: Iterator[A] = new AbstractIterator[A] {
    var these = self
    def hasNext: Boolean = !these.isEmpty
    def next(): A =
      if (hasNext) {
        val result = these.head; these = these.tail; result
      } else Iterator.empty.next()

    override def toList: List[A] = {
      /* Have to clear `these` so the iterator is exhausted like
       * it would be without the optimization.
       *
       * Calling "newBuilder.result()" in toList method
       * prevents original seq from garbage collection,
       * so we use these.take(0) here.
       *
       * Check SI-8924 for details
       */
      val xs = these.toList
      these = these.take(0)
      xs
    }
  }

1 个答案:

答案 0 :(得分:1)

Ravi是Scala自我类型..它可以用于依赖注入。所以截至目前它并没有指向任何真实类型,但使用代码可以传递符合该特征的具体类型。
请参阅this