Skip to content

Commit e7a0d03

Browse files
committed
Create FunctionN types on demand
We know create FunctionN types on demand whenever their name is looked up in the scope of package `scala`. This obviates the need to predefine function traits 23 to 30.
1 parent f7001a4 commit e7a0d03

File tree

11 files changed

+26
-164
lines changed

11 files changed

+26
-164
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,23 @@ class Definitions {
767767

768768
// ----- Initialization ---------------------------------------------------
769769

770+
/** Give the scala package a scope where a FunctionN trait is automatically
771+
* added when someone looks for it.
772+
*/
773+
private def makeScalaSpecial()(implicit ctx: Context) = {
774+
val oldInfo = ScalaPackageClass.classInfo
775+
val oldDecls = oldInfo.decls
776+
val newDecls = new MutableScope(oldDecls) {
777+
override def lookupEntry(name: Name)(implicit ctx: Context): ScopeEntry = {
778+
val res = super.lookupEntry(name)
779+
if (res == null && name.functionArity > 0)
780+
newScopeEntry(newFunctionNTrait(name.functionArity))
781+
else res
782+
}
783+
}
784+
ScalaPackageClass.info = oldInfo.derivedClassInfo(decls = newDecls)
785+
}
786+
770787
/** Lists core classes that don't have underlying bytecode, but are synthesized on-the-fly in every reflection universe */
771788
lazy val syntheticScalaClasses = List(
772789
AnyClass,
@@ -794,6 +811,8 @@ class Definitions {
794811
def init()(implicit ctx: Context) = {
795812
this.ctx = ctx
796813
if (!_isInitialized) {
814+
makeScalaSpecial()
815+
797816
// force initialization of every symbol that is synthesized or hijacked by the compiler
798817
val forced = syntheticCoreClasses ++ syntheticCoreMethods ++ ScalaValueClasses()
799818

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@ object NameOps {
229229
}
230230
}
231231

232+
def functionArity: Int =
233+
if (name.startsWith(tpnme.Function))
234+
try name.drop(tpnme.Function.length).toString.toInt
235+
catch { case ex: NumberFormatException => -1 }
236+
else -1
237+
232238
/** The name of the generic runtime operation corresponding to an array operation */
233239
def genericArrayOp: TermName = name match {
234240
case nme.apply => nme.array_apply

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ object Scopes {
309309

310310
/** Lookup a symbol entry matching given name.
311311
*/
312-
override final def lookupEntry(name: Name)(implicit ctx: Context): ScopeEntry = {
312+
override def lookupEntry(name: Name)(implicit ctx: Context): ScopeEntry = {
313313
var e: ScopeEntry = null
314314
if (hashTable ne null) {
315315
e = hashTable(name.hashCode & (hashTable.length - 1))

library/src/scala/Function23.scala

Lines changed: 0 additions & 21 deletions
This file was deleted.

library/src/scala/Function24.scala

Lines changed: 0 additions & 21 deletions
This file was deleted.

library/src/scala/Function25.scala

Lines changed: 0 additions & 21 deletions
This file was deleted.

library/src/scala/Function26.scala

Lines changed: 0 additions & 20 deletions
This file was deleted.

library/src/scala/Function27.scala

Lines changed: 0 additions & 20 deletions
This file was deleted.

library/src/scala/Function28.scala

Lines changed: 0 additions & 20 deletions
This file was deleted.

library/src/scala/Function29.scala

Lines changed: 0 additions & 20 deletions
This file was deleted.

library/src/scala/Function30.scala

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)