"narrow" was recently added to the macro/reflection API:
but I'm struggling to understand what it does, exactly, and how to use it. In particular, Martin suggested that I could use it to solve a particular issue I was having with parameterised members, but I'm struggling to do so.
Specifically, given:
scala> import reflect.runtime.universe._
import reflect.runtime.universe._
scala> class Outer[T] { trait Inner[T]; def m(): Inner[T] = null }
defined class Outer
scala> val t = typeOf[Outer[Int]]
t: reflect.runtime.universe.Type = Outer[Int]
scala> val m = t.member(newTermName("m"))
m: reflect.runtime.universe.Symbol = method m
scala> m.typeSignature
res0: reflect.runtime.universe.Type = ()Outer.this.Inner[T]
I'm trying to get the type of m with T substituted for Int. If I call narrow, I get:
scala> m.typeSignature.narrow
res1: reflect.runtime.universe.Type = <none>.<refinement>.type
It's not clear to me what I can do with this type - ultimately what I need to do is get the types of the arguments to the method and its return type (in this case, ()Outer.this.Inner[Int]). How do I do this with the type that's returned from narrow?
For reference, I can do it using the following hack, but Martin indicated that narrow would allow me to avoid this :-)
scala> m.typeSignatureIn(SuperType(ThisType(t.typeSymbol), t))
res2: reflect.runtime.universe.Type = ()Outer.this.Inner[Int]