2014-04-20 3 views
1

저는 컴파일러 용 새로운 API를 가지고 놀았으며 2.11에서 다시 돌아 왔고 이상한 결과를 보았습니다.스칼라 IMain` v. 2.11의`(Int)`와`Int` 타입의 차이점은 무엇입니까?

Welcome to Scala version 2.11.0-20140415-163722-cac6383e66 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_51). 
Type in expressions to have them evaluated. 
Type :help for more information. 

scala> import scala.tools.nsc.interpreter.IMain 
import scala.tools.nsc.interpreter.IMain 

scala> import scala.tools.nsc.Settings 
import scala.tools.nsc.Settings 

scala> val eng = new IMain(new IMain.Factory(), new Settings()) 
eng: scala.tools.nsc.interpreter.IMain = [email protected] 

scala> eng.interpret("val x: Int = 2") 
x: Int = 2 
res0: scala.tools.nsc.interpreter.IR.Result = Success 

scala> eng.valueOfTerm("x") 
res2: Option[Any] = Some(2) 

scala> eng.typeOfTerm("x") 
res3: eng.global.Type =()Int 

scala> eng.typeOfExpression("x") 
res4: eng.global.Type = Int 

scala> eng.typeOfExpression("x") =:= eng.global.definitions.IntTpe 
res6: Boolean = true 

scala> eng.typeOfTerm("x") =:= eng.global.definitions.IntTpe 
res7: Boolean = false 

당신이 볼 수 있듯이, typeOfTerm("x") 반환 ()Int하지만 typeOfExpression("x") 반환 Int : 여기 내 REPL 출력입니다. 나는 유형 ()Int이 유형 Int의 변수를 나타내는 경우라고 생각하지만, 나는 확신 할 수 없다. 누군가가 내 혼란을 확인하거나 교정 할 수 있고, 이에 대해 이야기하는 문서로 안내 할 수 있다면, 나는 그것을 고맙게 생각한다. 내가 찾을 수있는 반성 문서를 조사했지만 운이 없었습니다.

답변

1

REPL에서 val x은 로컬 정의가 아닙니다. 그것은 클래스 또는 객체로 싸여 있으며, 다른 행동으로 연결됩니다.

object X { val x = 2 }에는 마치 def x = 2 인 것처럼 매개 변수없는 메서드 유형 인 x이 있습니다.

scala> val x: Int = 2 
x: Int = 2 

scala> $intp.typeOfTerm("x") 
res0: $intp.global.Type =()Int 

scala> import $intp.global._ 
import $intp.global._ 

scala> $intp.replScope.lookup(TermName("x")) 
res2: $intp.global.Symbol = value x 

scala> res2.tpe 
res3: $intp.global.Type =()Int 

scala> :power 
** Power User mode enabled - BEEP WHIR GYVE ** 
** :phase has been set to 'typer'.   ** 
** scala.tools.nsc._ has been imported  ** 
** global._, definitions._ also imported ** 
** Try :help, :vals, power.<tab>   ** 

scala> intp.replScope.lookup(TermName("x")) 
res6: $r.intp.global.Symbol = value x 

이 우리가 그것을보고 사용하던 방법이다 : 나는 물론, 포장 객체에 대해 잊고 있었던

scala> .tpe 
res7: $r.intp.global.Type = => Int 

scala> :phase 
Active phase is 'Typer'. (To clear, :phase clear) 

scala> :phase clear 
Cleared active phase. 

scala> res6.tpe 
res8: $r.intp.global.Type =()Int 
+0

. 바보 같아요. 포장 개체가 만들어져 통역사에게 전송되는 방법을 추적하는 것을 기억하기 때문입니다. 감사! – TOB

관련 문제