2012-04-03 11 views
11

Alcune cose nella Scala sembra opaco a me come il seguente, quando to non è una funzione membro di Int:Come esaminare conversioni implicite/rich e attuate tratti in REPL

1.to(4) 

Posso esaminare ciò che il comportamento causato questo (una conversione implicita o un tratto o altro) senza consultare il riferimento del linguaggio? E anche nello REPL?

Se il REPL non può aiutare, c'è qualche alternativa amichevole?

risposta

14

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

Con Scala 2.10 o 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

Vedi anche ': impliciti -V' su REPL. –