@@ -2512,8 +2512,6 @@ object Types {
2512
2512
(paramBoundsExp : GenericType => List [TypeBounds ],
2513
2513
resultTypeExp : GenericType => Type )
2514
2514
extends CachedProxyType with BindingType with TermType {
2515
- type This <: GenericType
2516
- protected [this ] def companion : GenericCompanion [This ]
2517
2515
2518
2516
/** The bounds of the type parameters */
2519
2517
val paramBounds : List [TypeBounds ] = paramBoundsExp(this )
@@ -2536,10 +2534,7 @@ object Types {
2536
2534
paramBounds.mapConserve(_.substParams(this , argTypes).bounds)
2537
2535
2538
2536
/** Unconditionally create a new generic type like this one with given elements */
2539
- def newLikeThis (paramNames : List [TypeName ] = this .paramNames, paramBounds : List [TypeBounds ] = this .paramBounds, resType : Type )(implicit ctx : Context ): This =
2540
- companion.apply(paramNames, variances)(
2541
- x => paramBounds mapConserve (_.subst(this , x).bounds),
2542
- x => resType.subst(this , x))
2537
+ def newLikeThis (paramNames : List [TypeName ] = this .paramNames, paramBounds : List [TypeBounds ] = this .paramBounds, resType : Type )(implicit ctx : Context ): GenericType
2543
2538
2544
2539
def derivedGenericType (paramNames : List [TypeName ] = this .paramNames,
2545
2540
paramBounds : List [TypeBounds ] = this .paramBounds,
@@ -2584,23 +2579,20 @@ object Types {
2584
2579
}
2585
2580
2586
2581
/** A type for polymorphic methods */
2587
- class PolyType (paramNames : List [TypeName ])(paramBoundsExp : GenericType => List [TypeBounds ], resultTypeExp : GenericType => Type )
2588
- extends GenericType (paramNames)(paramBoundsExp, resultTypeExp) with MethodOrPoly {
2582
+ class PolyType (paramNames : List [TypeName ], variances : List [ Int ] )(paramBoundsExp : GenericType => List [TypeBounds ], resultTypeExp : GenericType => Type )
2583
+ extends TypeLambda (paramNames, variances )(paramBoundsExp, resultTypeExp) with MethodOrPoly {
2589
2584
2590
- type This = PolyType
2591
- def companion = PolyType
2585
+ protected override def computeSignature (implicit ctx : Context ) = resultSignature
2592
2586
2593
- protected def computeSignature (implicit ctx : Context ) = resultSignature
2594
-
2595
- def isPolymorphicMethodType : Boolean = resType match {
2587
+ override def isPolymorphicMethodType : Boolean = resType match {
2596
2588
case _ : MethodType => true
2597
2589
case _ => false
2598
2590
}
2599
2591
2600
2592
/** Merge nested polytypes into one polytype. nested polytypes are normally not supported
2601
2593
* but can arise as temporary data structures.
2602
2594
*/
2603
- def flatten (implicit ctx : Context ): PolyType = resType match {
2595
+ override def flatten (implicit ctx : Context ): PolyType = resType match {
2604
2596
case that : PolyType =>
2605
2597
val shift = new TypeMap {
2606
2598
def apply (t : Type ) = t match {
@@ -2615,12 +2607,17 @@ object Types {
2615
2607
case _ => this
2616
2608
}
2617
2609
2610
+ override def newLikeThis (paramNames : List [TypeName ], paramBounds : List [TypeBounds ], resType : Type )(implicit ctx : Context ): PolyType =
2611
+ PolyType .apply(paramNames, variances)(
2612
+ x => paramBounds mapConserve (_.subst(this , x).bounds),
2613
+ x => resType.subst(this , x))
2614
+
2618
2615
override def toString = s " PolyType( $paramNames, $paramBounds, $resType) "
2619
2616
}
2620
2617
2621
2618
object PolyType extends GenericCompanion [PolyType ] {
2622
2619
def apply (paramNames : List [TypeName ], variances : List [Int ] = Nil )(paramBoundsExp : GenericType => List [TypeBounds ], resultTypeExp : GenericType => Type )(implicit ctx : Context ): PolyType = {
2623
- unique(new PolyType (paramNames)(paramBoundsExp, resultTypeExp))
2620
+ unique(new PolyType (paramNames, paramNames.map(alwaysZero) )(paramBoundsExp, resultTypeExp))
2624
2621
}
2625
2622
}
2626
2623
@@ -2629,17 +2626,26 @@ object Types {
2629
2626
/** A type lambda of the form `[v_0 X_0, ..., v_n X_n] => T` */
2630
2627
class TypeLambda (paramNames : List [TypeName ], override val variances : List [Int ])(
2631
2628
paramBoundsExp : GenericType => List [TypeBounds ], resultTypeExp : GenericType => Type )
2632
- extends GenericType (paramNames)(paramBoundsExp, resultTypeExp) {
2629
+ extends GenericType (paramNames)(paramBoundsExp, resultTypeExp) with MethodOrPoly {
2633
2630
2634
2631
assert(resType.isInstanceOf [TermType ], this )
2635
2632
assert(paramNames.nonEmpty)
2636
2633
2637
- type This = TypeLambda
2638
- def companion = TypeLambda
2634
+ protected def computeSignature (implicit ctx : Context ) = resultSignature
2635
+
2636
+ def isPolymorphicMethodType : Boolean = resType match {
2637
+ case _ : MethodType => true
2638
+ case _ => false
2639
+ }
2639
2640
2640
2641
lazy val typeParams : List [LambdaParam ] =
2641
2642
paramNames.indices.toList.map(new LambdaParam (this , _))
2642
2643
2644
+ override def newLikeThis (paramNames : List [TypeName ], paramBounds : List [TypeBounds ], resType : Type )(implicit ctx : Context ): TypeLambda =
2645
+ TypeLambda .apply(paramNames, variances)(
2646
+ x => paramBounds mapConserve (_.subst(this , x).bounds),
2647
+ x => resType.subst(this , x))
2648
+
2643
2649
def derivedLambdaAbstraction (paramNames : List [TypeName ], paramBounds : List [TypeBounds ], resType : Type )(implicit ctx : Context ): Type =
2644
2650
resType match {
2645
2651
case resType @ TypeAlias (alias) =>
@@ -2652,14 +2658,33 @@ object Types {
2652
2658
derivedGenericType(paramNames, paramBounds, resType)
2653
2659
}
2654
2660
2661
+ /** Merge nested polytypes into one polytype. nested polytypes are normally not supported
2662
+ * but can arise as temporary data structures.
2663
+ */
2664
+ def flatten (implicit ctx : Context ): TypeLambda = resType match {
2665
+ case that : PolyType =>
2666
+ val shift = new TypeMap {
2667
+ def apply (t : Type ) = t match {
2668
+ case PolyParam (`that`, n) => PolyParam (that, n + paramNames.length)
2669
+ case t => mapOver(t)
2670
+ }
2671
+ }
2672
+ PolyType (paramNames ++ that.paramNames)(
2673
+ x => this .paramBounds.mapConserve(_.subst(this , x).bounds) ++
2674
+ that.paramBounds.mapConserve(shift(_).subst(that, x).bounds),
2675
+ x => shift(that.resultType).subst(that, x).subst(this , x))
2676
+ case _ => this
2677
+ }
2678
+
2655
2679
override def toString = s " TypeLambda( $variances, $paramNames, $paramBounds, $resType) "
2656
2680
}
2657
2681
2658
2682
object TypeLambda extends GenericCompanion [TypeLambda ] {
2659
- def apply (paramNames : List [TypeName ], variances : List [Int ])(
2683
+ def apply (paramNames : List [TypeName ], variances : List [Int ] = Nil )(
2660
2684
paramBoundsExp : GenericType => List [TypeBounds ],
2661
2685
resultTypeExp : GenericType => Type )(implicit ctx : Context ): TypeLambda = {
2662
- unique(new TypeLambda (paramNames, variances)(paramBoundsExp, resultTypeExp))
2686
+ val vs = if (variances.isEmpty) paramNames.map(alwaysZero) else variances
2687
+ unique(new TypeLambda (paramNames, vs)(paramBoundsExp, resultTypeExp))
2663
2688
}
2664
2689
2665
2690
def unapply (tl : TypeLambda ): Some [(List [LambdaParam ], Type )] =
0 commit comments