2012-04-03 16 views
11

Một số điều trong Scala dường như mờ đục với tôi như sau khi to không phải là một hàm thành viên của Int:Làm thế nào để kiểm tra ngầm/chuyển đổi phong phú và thực hiện những đặc điểm trong REPL

1.to(4) 

Tôi có thể kiểm tra những gì hành vi gây ra điều này (một chuyển đổi hoặc đặc điểm tiềm ẩn hoặc khác) mà không tham khảo tài liệu tham khảo ngôn ngữ? Và điều đó nữa trong số REPL?

Nếu REPL không thể trợ giúp, có một số thay thế thân thiện không?

Trả lời

14

Với 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(); 
     () 
     } 
     } 
    } 
    } 
} 

Với Scala 2.10 hay 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

Xem thêm ': implicits -v' trên REPL. –

Các vấn đề liên quan