Skip to content

Add documentation on Repeated and RepeatedParamClass #18792

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
Nov 8, 2023
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
30 changes: 29 additions & 1 deletion library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1622,7 +1622,30 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
end extension
end ReturnMethods

/** Tree representing a variable argument list in the source code */
/** Tree representing a variable argument list in the source code.
*
* This tree is used to encode varargs terms. The Repeated encapsulates
* the sequence of the elements but needs to be wrapped in a
* `scala.<repeated>[T]` (see `defn.RepeatedParamClass`). For example the
* arguments `1, 2` of `List.apply(1, 2)` can be represented as follows:
*
*
* ```scala
* //{
* import scala.quoted._
* def inQuotes(using Quotes) = {
* val q: Quotes = summon[Quotes]
* import q.reflect._
* //}
* val intArgs = List(Literal(Constant(1)), Literal(Constant(2)))
* Typed(
* Repeated(intArgs, TypeTree.of[Int]),
* Inferred(defn.RepeatedParamClass.typeRef.appliedTo(TypeRepr.of[Int]))
* //{
* }
* //}
* ```
*/
type Repeated <: Term

/** `TypeTest` that allows testing at runtime in a pattern match if a `Tree` is a `Repeated` */
Expand All @@ -1633,8 +1656,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>

/** Methods of the module object `val Repeated` */
trait RepeatedModule { this: Repeated.type =>
/** Create a literal sequence of elements */
def apply(elems: List[Term], tpt: TypeTree): Repeated
/** Copy a literal sequence of elements */
def copy(original: Tree)(elems: List[Term], tpt: TypeTree): Repeated
/** Matches a literal sequence of elements */
def unapply(x: Repeated): (List[Term], TypeTree)
}

Expand Down Expand Up @@ -4314,6 +4340,8 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>

/** A dummy class symbol that is used to indicate repeated parameters
* compiled by the Scala compiler.
*
* @see Repeated
*/
def RepeatedParamClass: Symbol

Expand Down
20 changes: 20 additions & 0 deletions tests/pos/i18784/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import scala.quoted.*

object Macro {
inline def repeated = ${Macro.repeatedImpl}
def repeatedImpl(using Quotes):Expr[List[Int]] = {
import quotes.reflect.*
val args = List(Expr(1), Expr(2))
val listObjectTerm = '{ List }.asTerm
Apply(
TypeApply(
Select.unique(listObjectTerm, "apply"),
List(TypeTree.of[Int])
),
List(
Typed(
Repeated(args.map(_.asTerm), TypeTree.of[Int]),
Inferred(defn.RepeatedParamClass.typeRef.appliedTo(TypeRepr.of[Int]))))
).asExprOf[List[Int]]
}
}
2 changes: 2 additions & 0 deletions tests/pos/i18784/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def Test: Unit =
Macro.repeated