【问题标题】:is a method parameter implicit? - using scala 2.10 reflection方法参数是隐含的吗? - 使用 scala 2.10 反射
【发布时间】:2012-08-10 17:57:34
【问题描述】:
给定一个反射方法:
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
找出方法是否具有隐式参数的最佳方法是什么(在 scala 2.10-M4+ 中)?
【问题讨论】:
标签:
scala
reflection
scala-2.10
【解决方案1】:
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
如果您问我更喜欢哪种方式,我建议您使用isXXX 方法。首先,这是一种一致的测试方式,因为只有十几个公共标志,但要测试的东西要多得多(例如isStable 或isSynthetic)。其次,标志比看起来更复杂(例如,多个标志名称可能对应于标志掩码中的同一位),因此isXXX 方法提供了更好的封装。