Domanda

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+)?

È stato utile?

Soluzione

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.

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top