What are <stable> and <accessor> when doing Scalac -Xprint:typer?
Asked Answered
M

2

7

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...

Maddeu answered 2/8, 2013 at 18:23 Comment(0)
S
5

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.

Swarey answered 2/8, 2013 at 20:10 Comment(0)
P
0

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.

Ptolemaeus answered 13/11, 2013 at 15:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.