is a method parameter implicit? - using scala 2.10 reflection
-
24-06-2021 - |
Pregunta
Given a reflected method:
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
What's the best way to find out if the method has implicit parameters (in scala 2.10-M4+)?
Solución
Right now, I don't think that you can. isImplicit
was added 6 days ago, so you should be able to with a nightly build or in the next milestone release.
Otros consejos
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
If you ask me what way to prefer, I'd advise to go with isXXX
methods. First of all, that's a consistent way of testing, because there's only a dozen or so public flags, but there are much more things to test (e.g. isStable
or isSynthetic
). Secondly, flags are trickier than they look (e.g. several flag names might correspond to the same bit in the flag mask), so isXXX
methods provide better encapsulation.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow