Question

I've written a tiny bit of Scala

object SquareNumbers extends App {
  val numbers = List(1,2,3,4,5)
  val squares = numbers map (i => i * i)
  println (squares)
}

And run it through scalac as so:

$ scalac -Xprint:typer SquareNumbers.scala
[[syntax trees at end of                     typer]] // SquareNumbers.scala
package <empty> {
  object SquareNumbers extends Object with App {
    def <init>(): SquareNumbers.type = {
      SquareNumbers.super.<init>();
      ()
    };
    private[this] val numbers: List[Int] = immutable.this.List.apply[Int](1, 2, 3, 4, 5);
    <stable> <accessor> def numbers: List[Int] = SquareNumbers.this.numbers;
    private[this] val squares: List[Int] = SquareNumbers.this.numbers.map[Int, List[Int]](((i: Int) => i.*(i)))(immutable.this.List.canBuildFrom[Int]);
    <stable> <accessor> def squares: List[Int] = SquareNumbers.this.squares;
    scala.this.Predef.println(SquareNumbers.this.squares)
  }
}

My question is, what are <stable> and <accessor> in the output? What are they called (as in, do they have a collective noun), and what do they do?

At a guess, I'd say that meant they were vals instead of vars, and mean that it was a callable from outside of the object...

Was it helpful?

Solution

These are internal (i.e., not exposed through the new 2.10 reflection API) flags. The official compiler ScalaDoc site seems to be down, but you can see the Scala source for details:

final val STABLE   = 1 << 22 // functions that are assumed to be stable
                             // (typically, access methods for valdefs)
                             // or classes that do not contain abstract types.

And:

final val ACCESSOR = 1 << 27 // a value or variable accessor (getter or setter)

Later in that file you can find the mapping between identifiers (e.g. STABLE) and the printed strings (<stable>), lists of which flags are displayed at which phases, etc.

OTHER TIPS

The meaning of ACCESSOR is pretty obvious, but that of STABLE is not.

AFAICT, STABLE denotes a getter of an immutable field (i.e., a val), or a method parameter, which is similarly immutable within the scope of the method. I'd guess that this is used to optimize by eliminating reevaluations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top