通常,使用提取器。有時我必須查看運行時類是什麼,才能記住要使用的提取器。我不會每天都使用這個API。
scala> import reflect.runtime._, universe._
import reflect.runtime._
import universe._
scala> class X { def x(i: => Int) = i * 2 }
defined class X
scala> typeOf[X].member(TermName("x"))
res0: reflect.runtime.universe.Symbol = method x
scala> .typeSignature
res1: reflect.runtime.universe.Type = (i: => scala.Int)scala.Int
scala> res1 match { case MethodType(ps, res) => ps }
res2: List[reflect.runtime.universe.Symbol] = List(value i)
scala> .head
res3: reflect.runtime.universe.Symbol = value i
scala> .typeSignature
res4: reflect.runtime.universe.Type = => scala.Int
scala> res4.getClass
res5: Class[_ <: reflect.runtime.universe.Type] = class scala.reflect.internal.Types$ClassArgsTypeRef
scala> res4 match { case TypeRef(pre, sym, args) => sym }
res6: reflect.runtime.universe.Symbol = class <byname>
scala> res4 match { case TypeRef(pre, sym, args) => args }
res7: List[reflect.runtime.universe.Type] = List(scala.Int)
scala> definitions
res8: reflect.runtime.universe.DefinitionsApi = [email protected]
scala> definitions.By
ByNameParamClass ByteClass ByteTpe
scala> definitions.ByNameParamClass
res9: reflect.runtime.universe.ClassSymbol = class <byname>
我依稀記得特殊的名字,但是我得到一個穩定的模式匹配前綴嗎?我猜不會。
scala> res4 match { case TypeRef(pre, definitions.ByNameParamClass, args) => args }
<console>:20: error: stable identifier required, but scala.reflect.runtime.`package`.universe.definitions.ByNameParamClass found.
Note that method ByNameParamClass is not stable because its type, => reflect.runtime.universe.ClassSymbol, is volatile.
res4 match { case TypeRef(pre, definitions.ByNameParamClass, args) => args }
^
scala> val k = definitions.ByNameParamClass
k: reflect.runtime.universe.ClassSymbol = class <byname>
scala> res4 match { case TypeRef(pre, k, args) => args }
res11: List[reflect.runtime.universe.Type] = List(scala.Int)
scala>
謝謝,它使用'c.universe.definitions.ByNameParamClass'。我接近找到解決方案,通過修復repl,但不是那裏! –