Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to examine implicit/rich conversions and implemented traits in the REPL

Tags:

scala

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?

like image 762
Jesvin Jose Avatar asked Apr 03 '12 19:04

Jesvin Jose


1 Answers

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)
like image 153
retronym Avatar answered Nov 16 '22 02:11

retronym