This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Misunderstanding instantiateTypeParams

4 replies
paulbutcher
Joined: 2010-03-08,
User offline. Last seen 10 weeks 5 days ago.

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: paul@paulbutcher.com
AIM: paulrabutcher
Skype: paulrabutcher

adriaanm
Joined: 2010-02-08,
User offline. Last seen 31 weeks 4 days ago.
Re: Misunderstanding instantiateTypeParams


On Sun, Nov 20, 2011 at 6:33 PM, Paul Butcher <paul@paulbutcher.com> wrote:
What am I missing?
appliedType
paulbutcher
Joined: 2010-03-08,
User offline. Last seen 10 weeks 5 days ago.
Re: Misunderstanding instantiateTypeParams
On 20 Nov 2011, at 21:53, Adriaan Moors wrote:
On Sun, Nov 20, 2011 at 6:33 PM, Paul Butcher <paul@paulbutcher.com> wrote:
What am I missing?
appliedType

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.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: paul@paulbutcher.com
AIM: paulrabutcher
Skype: paulrabutcher
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Misunderstanding instantiateTypeParams

On Sun, Nov 20, 2011 at 2:57 PM, Paul Butcher 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)

paulbutcher
Joined: 2010-03-08,
User offline. Last seen 10 weeks 5 days ago.
Re: Misunderstanding instantiateTypeParams

Many thanks, Paul - that makes perfect sense.

On 21 Nov 2011, at 03:57, Paul Phillips wrote:
> On Sun, Nov 20, 2011 at 2:57 PM, Paul Butcher 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->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: paul@paulbutcher.com
AIM: paulrabutcher
Skype: paulrabutcher

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland