What does narrow do and how should it be used?

145 views
Skip to first unread message

Paul Butcher

unread,
Aug 2, 2012, 9:13:43 AM8/2/12
to scala...@googlegroups.com, Eugene Burmako
"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]

--
paul.butcher->msgCount++

Snetterton, Castle Combe, Cadwell Park...
Who says I have a one track mind?

http://www.paulbutcher.com/
LinkedIn: http://www.linkedin.com/in/paulbutcher
MSN: pa...@paulbutcher.com
AIM: paulrabutcher
Skype: paulrabutcher

Eugene Burmako

unread,
Aug 3, 2012, 5:45:46 AM8/3/12
to Paul Butcher, scala...@googlegroups.com
Hi Paul,

I'm currently swamped, so I'll answer only during the weekend or later. Okay?

Cheers,
Eugene

Paul Butcher

unread,
Aug 3, 2012, 6:54:56 AM8/3/12
to Eugene Burmako, scala...@googlegroups.com
On 3 Aug 2012, at 10:45, Eugene Burmako <eugene....@epfl.ch> wrote:

I'm currently swamped, so I'll answer only during the weekend or later. Okay?

Absolutely - there's no rush associated with this - I have a working solution (even though Martin considers it to be a hack :-)

Paul Butcher

unread,
Aug 11, 2012, 1:44:59 PM8/11/12
to Eugene Burmako, scala...@googlegroups.com
Just a gentle ping to see if you've had a chance to think about this yet, Eugene?

--
paul.butcher->msgCount++

Snetterton, Castle Combe, Cadwell Park...
Who says I have a one track mind?

http://www.paulbutcher.com/
LinkedIn: http://www.linkedin.com/in/paulbutcher
MSN: pa...@paulbutcher.com
AIM: paulrabutcher
Skype: paulrabutcher

Eugene Burmako

unread,
Aug 11, 2012, 6:10:19 PM8/11/12
to Paul Butcher, Paul Phillips, scala...@googlegroups.com
I don't know how this stuff is supposed to work, but will ask Martin next Thursday.

In the meanwhile, some experiments:

scala> m.typeSignatureIn(SuperType(ThisType(t.typeSymbol), t))
res0 @ 7bfbfeae: reflect.runtime.universe.Type = ()Outer.this.Inner[Int]

scala> m.typeSignatureIn(t.narrow)
res1 @ 1247de8f: reflect.runtime.universe.Type = ()this.Inner[Int]

scala> res0 =:= res1
res2 @ 2eb9e305: Boolean = false

scala> showRaw(res0)
res3 @ 6fa85031: String = MethodType(List(), TypeRef(ThisType(Outer), newTypeName("Inner"), List(Typ
eRef(ThisType(scala), scala.Int, List()))))

scala> showRaw(res1)
res4 @ 5ca32803: String = MethodType(List(), TypeRef(ThisType(newTypeName("<refinement>")), newTypeN
ame("Inner"), List(TypeRef(ThisType(scala), scala.Int, List()))))

@Paul Phillips, maybe you could chime in?

On 2 August 2012 15:13, Paul Butcher <pa...@paulbutcher.com> wrote:

Eugene Burmako

unread,
Sep 12, 2012, 5:04:09 PM9/12/12
to scala...@googlegroups.com, Paul Butcher, Paul Phillips
Sorry for the late reply, but I fail to understand how to get to ()Outer.this.Inner[Int]. 

We had a brief chat with Martin yesterday, and he also doesn't know how to implement this scenario, except for the hack you used.
Reply all
Reply to author
Forward
0 new messages