Skip to content

Add reflect AppliedType constructor #14923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1893,6 +1893,8 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
end AppliedTypeTypeTest

object AppliedType extends AppliedTypeModule:
def apply(tycon: TypeRepr, args: List[TypeRepr]): AppliedType =
Types.AppliedType(tycon, args)
def unapply(x: AppliedType): (TypeRepr, List[TypeRepr]) =
(AppliedTypeMethods.tycon(x), AppliedTypeMethods.args(x))
end AppliedType
Expand Down
3 changes: 3 additions & 0 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2812,6 +2812,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>

/** Methods of the module object `val AppliedType` */
trait AppliedTypeModule { this: AppliedType.type =>
/** Applied the type constructor `T` to a list of type arguments `T_1,..,T_n` to create `T[T_1,..,T_n]` */
@experimental
def apply(tycon: TypeRepr, args: List[TypeRepr]): AppliedType
def unapply(x: AppliedType): (TypeRepr, List[TypeRepr])
}

Expand Down
1 change: 1 addition & 0 deletions project/MiMaFilters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ object MiMaFilters {
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolMethods.typeRef"),
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolMethods.termRef"),
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeTreeModule.ref"),
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#AppliedTypeModule.apply"),

// Experimental `MainAnnotation` APIs. Can be added in 3.3.0 or later.
ProblemFilters.exclude[MissingClassProblem]("scala.annotation.MainAnnotation"),
Expand Down
11 changes: 11 additions & 0 deletions tests/pos-macros/i14740/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import scala.quoted.*

type Foo[T]

transparent inline def emptyList[T]: Any = ${ impl[T] }

private def impl[T: Type](using Quotes): Expr[Any] = {
import quotes.reflect._
val tpe = AppliedType(TypeRepr.of[Foo], List(TypeRepr.of[T])) // test AppliedType constructor
Typed('{???}.asTerm, Inferred(tpe)).asExpr // '{ ??? : Foo[T] }
}
3 changes: 3 additions & 0 deletions tests/pos-macros/i14740/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def test1: Foo[Any] = emptyList[Any]
def test2: Foo[Int] = emptyList[Int]
def test3: Foo[String] = emptyList[String]