Misunderstanding instantiateTypeParams

88 views
Skip to first unread message

Paul Butcher

unread,
Nov 20, 2011, 5:33:18 PM11/20/11
to scala-i...@googlegroups.com
I'm clearly misunderstanding instantiateTypeParams, as it's not doing what I expect. I'd be very grateful for a pointer in the right direction.

I have a PolyType called tpe. I'd like to create a new type which is exactly the same as that type, but with each formal type parameter instantiated to Any. I thought that I could do this with:

> val actuals = List.fill(tpe.typeParams.length)(AnyClass.tpe)
> tpe.instantiateTypeParams(tpe.typeParams, actuals)

But the type I get back still seems to have the same typeParams :-(

For example, if I start with this:

> [A, B](x: Int, a: A, b: B)(Int, A, B)

After the above, I end up with:

> [A, B](x: Int, a: A, b: B)(Int, A, B)

But what I want to get is:

> (x: Int, a: Any, b: Any)(Int, Any, Any)

What am I missing?

Thanks in advance,

--
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

Adriaan Moors

unread,
Nov 20, 2011, 9:53:14 PM11/20/11
to scala-i...@googlegroups.com


On Sun, Nov 20, 2011 at 6:33 PM, Paul Butcher <pa...@paulbutcher.com> wrote:
What am I missing?
appliedType

Paul Butcher

unread,
Nov 20, 2011, 10:57:32 PM11/20/11
to scala-i...@googlegroups.com
Aha! That's done the trick. Thank you!

JOOI, what exactly does instantiateTypeParams do, then? It *looked* (and, to piggyback on a discussion going on "in another place", the types seemed to suggest :-) that it does exactly what appliedType does. But clearly not :-)

Paul Phillips

unread,
Nov 21, 2011, 3:57:07 AM11/21/11
to scala-i...@googlegroups.com
On Sun, Nov 20, 2011 at 2:57 PM, Paul Butcher <pa...@paulbutcher.com> wrote:
> Aha! That's done the trick. Thank you!
> JOOI, what exactly does instantiateTypeParams do, then? It *looked* (and, to
> piggyback on a discussion going on "in another place", the types seemed to
> suggest :-) that it does exactly what appliedType does. But clearly not :-)

It does do (a subset of) what applied type does - applied type calls it.

// this is the info for def f[A, B](x: Int, a: A, b: B): (Int, A, B)
scala> res0.info
res1: $r.intp.global.Type = [A<: <?>, B<: <?>](x: <?>, a: <?>, b:
<?>)(Int, A, B)

// This is you, trying to substitute into a polytype
scala> res0.info.instantiateTypeParams(res0.typeParams,
List(AnyClass.tpe, AnyClass.tpe))
res2: $r.intp.global.Type = [A, B](x: Int, a: A, b: B)(Int, A, B)

// This is applied type, substituting into a methodtype
scala> appliedType(res0.info, List(AnyClass.tpe, AnyClass.tpe))
res3: $r.intp.global.Type = (x: Int, a: Any, b: Any)(Int, Any, Any)

// This is you again after more closely imitating appliedType
scala> res0.info.resultType.instantiateTypeParams(res0.typeParams,
List(AnyClass.tpe, AnyClass.tpe))
res4: $r.intp.global.Type = (x: Int, a: Any, b: Any)(Int, Any, Any)

Paul Butcher

unread,
Nov 21, 2011, 9:22:13 AM11/21/11
to scala-i...@googlegroups.com
Many thanks, Paul - that makes perfect sense.

--

Reply all
Reply to author
Forward
0 new messages