简体   繁体   中英

how to use scala api document?

I've seen the args.foreach(arg => println(arg)), but when I search the doc http://www.scala-lang.org/api/current/index.html#scala.Array . I don't find it there and its companion object doc.

Any advice? Thanks

Array gets operations such as foreach from scala.collection.mutable.ArrayOps - so you can lookup ArrayOps in the documentation to see what methods are available on arrays.

Predef contains a number of implicit conversion methods to make these available for arrays.

Unfortunately you can't see in the Scala API documentation for Array that these methods are available via the implicits in Predef .

As Jesper said, it's not really easy to find documentation on methods acquired via implicit conversions. The ones defined for Array and the rationale behind them are explained here .

One (not particularly handy...) general way of finding out where a particular method comes from (in cases like yours, where it comes from an implicit conversion) is to use the -print option of the scala REPL.

If you run scala -print you will get:

scala> Array(1,2,3,4)
// ..... omitted for brevity
res0: Array[Int] = Array(1, 2, 3, 4)

scala> res0 foreach (println)
[[syntax trees at end of cleanup]]// Scala source: <console>
package $line2 {
  final object $read extends java.lang.Object with ScalaObject {
    def this(): object $line2.$read = {
      $read.super.this();
      ()
    }
  };
  @SerialVersionUID(0) final <synthetic> class $read$$iw$$iw$$anonfun$1 extends
scala.runtime.AbstractFunction1 with Serializable {
    final def apply(x: java.lang.Object): Unit = scala.this.Predef.println(x);
    final <bridge> def apply(v1: java.lang.Object): java.lang.Object = {
      $read$$iw$$iw$$anonfun$1.this.apply(v1);
      scala.runtime.BoxedUnit.UNIT
    };
    def this(): anonymous class $read$$iw$$iw$$anonfun$1 = {
      $read$$iw$$iw$$anonfun$1.super.this();
      ()
    }
  };
  final object $read$$iw$$iw extends java.lang.Object with ScalaObject {
    private[this] val res1: scala.runtime.BoxedUnit = _;
    <stable> <accessor> def res1(): Unit = ();
    def this(): object $line2.$read$$iw$$iw = {
      $read$$iw$$iw.super.this();
      $read$$iw$$iw.this.res1 = {
        scala.this.Predef.intArrayOps($line1.$read$$iw$$iw.res0()).foreach({
          {
            (new anonymous class $read$$iw$$iw$$anonfun$1(): Function1)
          }
        });
        scala.runtime.BoxedUnit.UNIT
      };
      ()
    }
  };
  final object $read$$iw extends java.lang.Object with ScalaObject {
    def this(): object $line2.$read$$iw = {
      $read$$iw.super.this();
      ()
    }
  }
}

[[syntax trees at end of cleanup]]// Scala source: <console>
package $line2 {
  final object $eval extends java.lang.Object with ScalaObject {
    @volatile protected var bitmap$0: Int = 0;
    <stable> <accessor> lazy def $result(): Unit = {
      if ($eval.this.bitmap$0.&(1).==(0))
        {
          $eval.this.synchronized({
            if ($eval.this.bitmap$0.&(1).==(0))
              {
                {
                  $eval.this.$print();
                  $line2.$read$$iw$$iw.res1()
                };
                $eval.this.bitmap$0 = $eval.this.bitmap$0.|(1);
                ()
              };
            scala.runtime.BoxedUnit.UNIT
          });
          ()
        };
      ()
    };
    private[this] val $print: java.lang.String = _;
    <stable> <accessor> def $print(): java.lang.String = $eval.this.$print;
    def this(): object $line2.$eval = {
      $eval.super.this();
      $eval.this.$print = {
        $line2.$read$$iw$$iw;
        ""
      };
      ()
    }
  }
}

1
2
3
4

If you look for .foreach inside this code, you will find the relevant line, that tells you the method is actually called on intArrayOps :

scala.this.Predef.intArrayOps($line1.$read$$iw$$iw.res0()).foreach(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM