- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Misunderstanding instantiateTypeParams
Sun, 2011-11-20, 18:33
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
Mon, 2011-11-21, 00:07
#2
Re: Misunderstanding instantiateTypeParams
On 20 Nov 2011, at 21:53, Adriaan Moors 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 :-)
--
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
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
Mon, 2011-11-21, 05:07
#3
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)
Mon, 2011-11-21, 10:27
#4
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
On Sun, Nov 20, 2011 at 6:33 PM, Paul Butcher <paul@paulbutcher.com> wrote:
appliedType