12

Some things in Scala seems opaque to me such as the following when to is not a member function of Int:

1.to(4)

Can I examine what behavior caused this (an implicit conversion or trait or other) without consulting the language reference? And that too in the REPL?

If the REPL can't help, is there some friendly alternative?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jesvin Jose
  • 22,498
  • 32
  • 109
  • 202

1 Answers1

14

With Scala 2.9:

  ~/code/scala scala -Xprint:typer -e "1 to 4"
[[syntax trees at end of typer]]// Scala source: scalacmd4469348504265784881.scala
package <empty> {
  final object Main extends java.lang.Object with ScalaObject {
    def this(): object Main = {
      Main.super.this();
      ()
    };
    def main(argv: Array[String]): Unit = {
      val args: Array[String] = argv;
      {
        final class $anon extends scala.AnyRef {
          def this(): anonymous class $anon = {
            $anon.super.this();
            ()
          };
          scala.this.Predef.intWrapper(1).to(4)
        };
        {
          new $anon();
          ()
        }
      }
    }
  }
}

With Scala 2.10 or 2.11:

scala> import reflect.runtime.universe
import reflect.runtime.universe

scala> val tree = universe.reify(1 to 4).tree
tree: reflect.runtime.universe.Tree = Predef.intWrapper(1).to(4)

scala> universe.showRaw(tree)
res0: String = Apply(Select(Apply(Select(Ident(scala.Predef), newTermName("intWrapper")), List(Literal(Constant(1)))), newTermName("to")), List(Literal(Constant(4))))

scala> universe.show(tree)
res1: String = Predef.intWrapper(1).to(4)
Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
retronym
  • 54,768
  • 12
  • 155
  • 168