매크로 주석 ("현재") 유형을 얻으려면 어떻게해야합니까?매크로 주석 유형
import scala.annotation.StaticAnnotation
import scala.reflect.macros._
import language.experimental.macros
class myself extends StaticAnnotation {
def macroTransform(annottees: Any*) = macro myselfMacro.impl
}
object myselfMacro {
def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
import c.universe._
val a = annottees.head
println(s"static type = ${a.staticType}")
println(s"actual type = ${a.actualType}")
c.Expr[Any](Literal(Constant()))
}
}
시험 :
@myself class Foo
출력 :
static type = Nothing
actual type = null
I 그 종류를 원하는 이유는 I는 예를 들어, 입력 파라미터로 사용하려는 것이다
Bar[Foo]
편집 :
좋아, 그래서 올바른 접근 방식은 다음과 같이 생각 :
def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
import c.universe._
val v = annottees.head.asInstanceOf[ClassDef]
val tpe = v.tpe // <- tpe is null as the annotated type is not yet type checked!
val tpe2 = if (tpe == null)
c.typeCheck(v).tpe // <- fails with a compiler error (assertion failure)
else
tpe
println(s"Type of annottee: $tpe2")
???
}
그러나 given this post by Eugene Burmako, 그것은 본다 ... it is currently not possible 같은
매크로 주석이 2.11.x에서 발생하지 않는다고 생각했습니다. 그게 바뀌 었습니까? – wheaties
매크로 파라다이스 플러그인을 통해 2.10과 2.11에서 이미 발생했습니다. 우리가 표준 배포판에 포함되는 것에 대해 이야기하고 있다면, 바닐라 2.10도 바닐라 2.11도 그럴 것입니다. –
유스 케이스에 대해 자세히 설명해 주시겠습니까? 매크로 주석이 확장 될 때 annottee는 아직 생성되지 않은 기호 (때로는 표시되지만 지정되지는 않음)가 없습니다. 따라서 심볼/유형으로는 트리별로 만 참조 할 수 없습니다. 물론, c.typecheck의 결과는 당신에게 어떤 기호/유형을 줄 것이다. 그러나 참조 목적이 아닌 내성을 위해서만 사용될 수있을 것이다. –