Skip to content

Commit 7f72143

Browse files
committed
Handling higher-kinded types with lambdas
Switch to the new scheme where higher-kinded types (and also some polymorphic type aliases) are represented as instances of Lambda traits.
1 parent 388d9a8 commit 7f72143

23 files changed

+430
-345
lines changed

src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -424,61 +424,6 @@ class Definitions {
424424

425425
def functionArity(tp: Type) = tp.dealias.argInfos.length - 1
426426

427-
// ----- Higher kinds machinery ------------------------------------------
428-
// tbr
429-
private var _hkTraits: Set[Symbol] = Set()
430-
431-
/** The set of HigherKindedXYZ traits encountered so far */
432-
def hkTraits: Set[Symbol] = _hkTraits
433-
434-
private var hkTraitOfArity = mutable.Map[List[Int], ClassSymbol]()
435-
436-
/** The HigherKinded trait corresponding to symbols `boundSyms` (which are assumed
437-
* to be the type parameters of a higher-kided type). This is a class symbol that
438-
* would be generated by the following schema.
439-
*
440-
* class HigherKindedXYZ { type v_n _$hk$0; ...; type v_n _$Hk$n }
441-
*
442-
* Here:
443-
*
444-
* - XYZ is a string with one letter for each variant of a bound symbols,
445-
* using `P` (positive variance), `N` (negative variance), `I` (invariant).
446-
* - v_i are the variances of the bound symbols (i.e. +, -, or empty).
447-
* - _$hk$i are higher-kinded parameter names, which are specially treated in type application.
448-
*/
449-
def hkTrait(vcs: List[Int]) = {
450-
451-
def varianceFlags(v: Int) = v match {
452-
case -1 => Contravariant
453-
case 0 => Covariant
454-
case 1 => EmptyFlags
455-
}
456-
457-
val completer = new LazyType {
458-
def complete(denot: SymDenotation)(implicit ctx: Context): Unit = {
459-
val cls = denot.asClass.classSymbol
460-
val paramDecls = newScope
461-
for (i <- 0 until vcs.length)
462-
newTypeParam(cls, tpnme.higherKindedParamName(i), EmptyFlags, paramDecls)
463-
denot.info = ClassInfo(ScalaPackageClass.thisType, cls, List(ObjectClass.typeRef), paramDecls)
464-
}
465-
}
466-
467-
val traitName = tpnme.higherKindedTraitName(vcs)
468-
469-
def createTrait = {
470-
val cls = newClassSymbol(
471-
ScalaPackageClass,
472-
traitName,
473-
Trait | Interface | Synthetic,
474-
completer)
475-
_hkTraits += cls
476-
cls
477-
}
478-
479-
hkTraitOfArity.getOrElseUpdate(vcs, createTrait)
480-
}
481-
482427
// ----- LambdaXYZ traits ------------------------------------------
483428

484429
private var myLambdaTraits: Set[Symbol] = Set()

src/dotty/tools/dotc/core/NameOps.scala

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,13 @@ object NameOps {
8888
}
8989

9090
/** Is this the name of a higher-kinded type parameter of a Lambda? */
91-
def isLambdaArgName = name.startsWith(tpnme.LAMBDA_ARG_PREFIX)
92-
def isHkParamName: Boolean = name(0) == '_' && name.startsWith(HK_PARAM_PREFIX) // tbr
91+
def isLambdaArgName =
92+
name(0) == tpnme.LAMBDA_ARG_PREFIXhead && name.startsWith(tpnme.LAMBDA_ARG_PREFIX)
9393

9494
/** The index of the higher-kinded type parameter with this name.
9595
* Pre: isLambdaArgName.
9696
*/
9797
def lambdaArgIndex: Int = name.drop(name.lastIndexOf('$') + 1).toString.toInt
98-
def hkParamIndex: Int = name.drop(name.lastIndexOf('$') + 1).toString.toInt // tbr
9998

10099
/** If the name ends with $nn where nn are
101100
* all digits, strip the $ and the digits.
@@ -179,19 +178,6 @@ object NameOps {
179178
}
180179
}
181180

182-
/** The variances of the higherKinded parameters of the trait named
183-
* by this name.
184-
* @pre The name is a higher-kinded trait name, i.e. it starts with HK_TRAIT_PREFIX
185-
*/
186-
def hkVariances: List[Int] = { // tbr
187-
def varianceOfSuffix(suffix: Char): Int = {
188-
val idx = tpnme.varianceSuffixes.indexOf(suffix)
189-
assert(idx >= 0)
190-
idx - 1
191-
}
192-
name.drop(tpnme.HK_TRAIT_PREFIX.length).toList.map(varianceOfSuffix)
193-
}
194-
195181
/** The name of the generic runtime operation corresponding to an array operation */
196182
def genericArrayOp: TermName = name match {
197183
case nme.apply => nme.array_apply

src/dotty/tools/dotc/core/StdNames.scala

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ object StdNames {
167167
final val REIFY_TREECREATOR_PREFIX: N = "$treecreator"
168168
final val REIFY_TYPECREATOR_PREFIX: N = "$typecreator"
169169
final val LAMBDA_ARG_PREFIX: N = "$hkArg$"
170+
final val LAMBDA_ARG_PREFIXhead: Char = LAMBDA_ARG_PREFIX.head
170171

171172
final val Any: N = "Any"
172173
final val AnyVal: N = "AnyVal"
@@ -250,8 +251,6 @@ object StdNames {
250251
val SKOLEM: N = "<skolem>"
251252
val SPECIALIZED_INSTANCE: N = "specInstance$"
252253
val THIS: N = "_$this"
253-
val HK_PARAM_PREFIX: N = "_$hk$" // tbr
254-
val HK_TRAIT_PREFIX: N = "$HigherKinded$" // tbr
255254

256255
final val Nil: N = "Nil"
257256
final val Predef: N = "Predef"
@@ -647,9 +646,6 @@ object StdNames {
647646
def syntheticTypeParamNames(num: Int): List[TypeName] =
648647
(0 until num).map(syntheticTypeParamName)(breakOut)
649648

650-
def higherKindedTraitName(vcs: List[Int]): TypeName = HK_TRAIT_PREFIX ++ vcs.map(varianceSuffix).mkString // tbr
651-
def higherKindedParamName(n: Int) = HK_PARAM_PREFIX ++ n.toString //tbr
652-
653649
def lambdaTraitName(vcs: List[Int]): TypeName = LambdaPrefix ++ vcs.map(varianceSuffix).mkString
654650
def lambdaArgName(n: Int) = LAMBDA_ARG_PREFIX ++ n.toString
655651

0 commit comments

Comments
 (0)