Seq.apply在Scala 2.9.2中去了哪里?

时间:2012-07-26 11:51:28

标签: scala

Scala 2.8.2有一个Seq.apply方法,因此您可以在REPL中编写以下内容:

val l = Seq(1, 2)
l: Seq[In] = List(1, 2)

这仍然适用于Scala 2.9.2,但让我感到困惑的是,根据文档,不再有scala.collection.Seq.apply方法了。

我使用scala -Xprint:typer进行了检查,这就是它打印的内容:

[[syntax trees at end of typer]]// Scala source: <console>
package $line14 {
  final object $read extends java.lang.Object with ScalaObject {
    def this(): object $line14.$read = {
      $read.super.this();
      ()
    };
    final object $iw extends java.lang.Object with ScalaObject {
      def this(): object $line14.$read.$iw = {
        $iw.super.this();
        ()
      };
      final object $iw extends java.lang.Object with ScalaObject {
        def this(): object $line14.$read.$iw.$iw = {
          $iw.super.this();
          ()
        };
        private[this] val l: Seq[Int] = collection.this.Seq.apply[Int](1, 2);
        <stable> <accessor> def l: Seq[Int] = $iw.this.l
      }
    }
  }
}

[[syntax trees at end of typer]]// Scala source: <console>
package $line14 {
  final object $eval extends java.lang.Object with ScalaObject {
    def this(): object $line14.$eval = {
      $eval.super.this();
      ()
    };
    lazy private[this] var $result: Seq[Int] = {
      $eval.this.$print;
      $line14.$read.$iw.$iw.l
    };
    private[this] val $print: String = {
      $read.$iw.$iw;
      "l: Seq[Int] = ".+(scala.runtime.ScalaRunTime.replStringOf($line14.$read.$iw.$iw.l, 1000))
    };
    <stable> <accessor> def $print: String = $eval.this.$print
  }
}

l: Seq[Int] = List(1, 2)

结果是有效的:

collection.this.Seq.apply[Int](1, 2)

表示它仍然调用Seq.apply,但这个方法在哪里?

1 个答案:

答案 0 :(得分:4)

这是Scaladoc 2.9.2中的一个错误。

我检查了scala-library.jar,发现伴随对象scala.collection.Seq间接扩展了scala.collection.generic.GenericCompanion [Seq],它有效地提供了方法签名:

def apply[A](elems: A*): Seq[A]

这反过来调用newBuilder,它在Seq中被覆盖以返回:

scala.collection.immutable.Seq.newBuilder[A]