是隐式的方法参数吗? - 使用scala 2.10反射

Sag*_*ich 2 reflection scala scala-2.10

给出一个反映的方法:

scala> val sortMethod = typeOf[::[_]].member(newTermName("sorted"))
sortMethod: reflect.runtime.universe.Symbol = method sorted

scala> sortMethod.typeSignature
res122: reflect.runtime.universe.Type = [B >: A](implicit ord: scala.math.Ordering[B])Repr
Run Code Online (Sandbox Code Playgroud)

找出方法是否具有隐式参数(在scala 2.10-M4 +中)的最佳方法是什么?

Eug*_*ako 6

scala> import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe._

scala> val sortMethod = typeOf[::[_]].member(newTermName("sorted")).asMethod
sortMethod: reflect.runtime.universe.MethodSymbol = method sorted

scala> sortMethod.params // `params` has been added only a few days ago
res0: List[List[reflect.runtime.universe.Symbol]] = List(List(value ord))

scala> sortMethod.params(0)(0).asTerm.isImplicit
res2: Boolean = true

scala> sortMethod.params(0)(0) hasFlag Flag.IMPLICIT
res3: Boolean = true
Run Code Online (Sandbox Code Playgroud)

如果你问我喜欢什么方式,我建议你选择isXXX方法.首先,这是一种一致的测试方式,因为只有十几个公共标志,但还有更多要测试的东西(例如isStable或者isSynthetic).其次,标志比它们看起来更棘手(例如,几个标志名称可能对应于标志掩码中的相同位),因此isXXX方法提供了更好的封装.