2012-04-03 9 views
11

Niektóre rzeczy w Scala wydaje nieprzejrzysty do mnie, takie jak następujące po to nie jest funkcją członkiem Int:Jak badać ukryte/bogate konwersji i wdrożone cechy w REPL

1.to(4) 

Mogę zbadać jakie zachowanie spowodował to (niejawną konwersję lub cechę lub inne) bez sprawdzania odniesienia do języka? I to też w REPL?

Jeśli REPL nie może pomóc, czy jest jakaś przyjazna alternatywa?

Odpowiedz

14

Z 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(); 
     () 
     } 
     } 
    } 
    } 
} 

Z Scala 2.10 lub 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) 
+3

Zobacz także ': implicits -V' na REPL. –