Skip to content

Commit 07df610

Browse files
committed
Add HKTypeLambda
1 parent 6d63321 commit 07df610

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2393,6 +2393,7 @@ object Types {
23932393

23942394
abstract class HKLambda extends CachedProxyType with LambdaType {
23952395
final override def computeHash = doHash(paramNames, resType, paramInfos)
2396+
final override def underlying(implicit ctx: Context) = resType
23962397
}
23972398

23982399
abstract class StarLambda extends CachedGroundType with LambdaType with TermType {
@@ -2653,6 +2654,30 @@ object Types {
26532654
}
26542655
}
26552656

2657+
class HKTypeLambda(val paramNames: List[TypeName])(
2658+
paramInfosExp: HKTypeLambda => List[TypeBounds], resultTypeExp: HKTypeLambda => Type)
2659+
extends HKLambda with TypeLambda {
2660+
type This = HKTypeLambda
2661+
def companion = HKTypeLambda
2662+
2663+
val paramInfos: List[TypeBounds] = paramInfosExp(this)
2664+
val resType: Type = resultTypeExp(this)
2665+
2666+
assert(resType.isInstanceOf[TermType], this)
2667+
assert(paramNames.nonEmpty)
2668+
2669+
/** The type `[tparams := paramRefs] tp`, where `tparams` can be
2670+
* either a list of type parameter symbols or a list of lambda parameters
2671+
*/
2672+
def lifted(tparams: List[ParamInfo], tp: Type)(implicit ctx: Context): Type =
2673+
tparams match {
2674+
case LambdaParam(poly, _) :: _ => tp.subst(poly, this)
2675+
case tparams: List[Symbol @unchecked] => tp.subst(tparams, paramRefs)
2676+
}
2677+
2678+
protected def prefixString = "HKTypeLambda"
2679+
}
2680+
26562681
/** A type lambda of the form `[X_0 B_0, ..., X_n B_n] => T`
26572682
* This is used both as a type of a polymorphic method and as a type of
26582683
* a higher-kinded type parameter. Variances are encoded in parameter
@@ -2673,17 +2698,12 @@ object Types {
26732698
type This = PolyType
26742699
def companion = PolyType
26752700

2676-
/** The bounds of the type parameters */
26772701
val paramInfos: List[TypeBounds] = paramInfosExp(this)
2678-
2679-
/** The result type of a PolyType / body of a type lambda */
26802702
val resType: Type = resultTypeExp(this)
26812703

26822704
assert(resType.isInstanceOf[TermType], this)
26832705
assert(paramNames.nonEmpty)
26842706

2685-
override def underlying(implicit ctx: Context) = resType
2686-
26872707
/** Merge nested polytypes into one polytype. nested polytypes are normally not supported
26882708
* but can arise as temporary data structures.
26892709
*/
@@ -2714,6 +2734,21 @@ object Types {
27142734
protected def prefixString = "PolyType"
27152735
}
27162736

2737+
object HKTypeLambda extends TypeLambdaCompanion[HKTypeLambda] {
2738+
def apply(paramNames: List[TypeName])(
2739+
paramInfosExp: HKTypeLambda => List[TypeBounds],
2740+
resultTypeExp: HKTypeLambda => Type)(implicit ctx: Context): HKTypeLambda = {
2741+
unique(new HKTypeLambda(paramNames)(paramInfosExp, resultTypeExp))
2742+
}
2743+
2744+
def unapply(tl: HKTypeLambda): Some[(List[LambdaParam], Type)] =
2745+
Some((tl.typeParams, tl.resType))
2746+
2747+
def any(n: Int)(implicit ctx: Context) =
2748+
apply(syntheticParamNames(n))(
2749+
pt => List.fill(n)(TypeBounds.empty), pt => defn.AnyType)
2750+
}
2751+
27172752
object PolyType extends TypeLambdaCompanion[PolyType] {
27182753
def apply(paramNames: List[TypeName])(
27192754
paramInfosExp: PolyType => List[TypeBounds],

0 commit comments

Comments
 (0)