From 89f7e23b29936eaf84df199d1951b0abc819ba17 Mon Sep 17 00:00:00 2001 From: soronpo Date: Tue, 21 Sep 2021 21:31:52 -0400 Subject: [PATCH] Add SkolemType in metaprogramming --- .../scala/quoted/runtime/impl/QuotesImpl.scala | 12 ++++++++++++ .../runtime/impl/printers/Extractors.scala | 2 ++ .../runtime/impl/printers/SourceCode.scala | 5 +++++ library/src/scala/quoted/Quotes.scala | 16 ++++++++++++++++ 4 files changed, 35 insertions(+) diff --git a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala index 80c6dc757aaa..a51ac42a1f66 100644 --- a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala +++ b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala @@ -2216,6 +2216,18 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler def unapply(x: NoPrefix): true = true end NoPrefix + type SkolemType = dotc.core.Types.SkolemType + + object SkolemTypeTypeTest extends TypeTest[TypeRepr, SkolemType]: + def unapply(x: TypeRepr): Option[SkolemType & x.type] = x match + case tpe: (Types.SkolemType & x.type) => Some(tpe) + case _ => None + end SkolemTypeTypeTest + + object SkolemType extends SkolemTypeModule: + def unapply(x : SkolemType) : Some[TypeRepr] = Some(x.info) + end SkolemType + type Constant = dotc.core.Constants.Constant object Constant extends ConstantModule diff --git a/compiler/src/scala/quoted/runtime/impl/printers/Extractors.scala b/compiler/src/scala/quoted/runtime/impl/printers/Extractors.scala index 0bea8f0ab643..eb0fe2f5ee03 100644 --- a/compiler/src/scala/quoted/runtime/impl/printers/Extractors.scala +++ b/compiler/src/scala/quoted/runtime/impl/printers/Extractors.scala @@ -240,6 +240,8 @@ object Extractors { this += "NoPrefix()" case MatchCase(pat, rhs) => this += "MatchCase(" += pat += ", " += rhs += ")" + case SkolemType(info) => + this += "SkolemType(" += info += ")" } def visitSignature(sig: Signature): this.type = { diff --git a/compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala b/compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala index 77a54e23ad61..444adff782e4 100644 --- a/compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala +++ b/compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala @@ -1234,6 +1234,11 @@ object SourceCode { this += " => " printType(rhs) + case SkolemType(info) => + this += "$skolem[" + printType(info) + this += "]" + case _ => throw new MatchError(tpe.show(using Printer.TypeReprStructure)) } diff --git a/library/src/scala/quoted/Quotes.scala b/library/src/scala/quoted/Quotes.scala index a1199fc42169..b1da7bbe2b67 100644 --- a/library/src/scala/quoted/Quotes.scala +++ b/library/src/scala/quoted/Quotes.scala @@ -3163,6 +3163,22 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching => def unapply(x: NoPrefix): true } + // ----- SkolemType ----------------------------------------------- + + /** SkolemType for a type selection */ + type SkolemType <: TypeRepr + + /** `TypeTest` that allows testing at runtime in a pattern match if a `TypeRepr` is a `SkolemType` */ + given SkolemTypeTypeTest: TypeTest[TypeRepr, SkolemType] + + /** Module object of `type SkolemType` */ + val SkolemType: SkolemTypeModule + + /** Methods of the module object `val SkolemType` */ + trait SkolemTypeModule { this: SkolemType.type => + def unapply(x: SkolemType): Some[TypeRepr] + } + /////////////// // CONSTANTS // ///////////////