Skip to content

Commit cee2610

Browse files
authored
Add documentation on Repeated and RepeatedParamClass (#18792)
Close #18784
2 parents dc0b43c + 9d57680 commit cee2610

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

library/src/scala/quoted/Quotes.scala

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1622,7 +1622,30 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
16221622
end extension
16231623
end ReturnMethods
16241624

1625-
/** Tree representing a variable argument list in the source code */
1625+
/** Tree representing a variable argument list in the source code.
1626+
*
1627+
* This tree is used to encode varargs terms. The Repeated encapsulates
1628+
* the sequence of the elements but needs to be wrapped in a
1629+
* `scala.<repeated>[T]` (see `defn.RepeatedParamClass`). For example the
1630+
* arguments `1, 2` of `List.apply(1, 2)` can be represented as follows:
1631+
*
1632+
*
1633+
* ```scala
1634+
* //{
1635+
* import scala.quoted._
1636+
* def inQuotes(using Quotes) = {
1637+
* val q: Quotes = summon[Quotes]
1638+
* import q.reflect._
1639+
* //}
1640+
* val intArgs = List(Literal(Constant(1)), Literal(Constant(2)))
1641+
* Typed(
1642+
* Repeated(intArgs, TypeTree.of[Int]),
1643+
* Inferred(defn.RepeatedParamClass.typeRef.appliedTo(TypeRepr.of[Int]))
1644+
* //{
1645+
* }
1646+
* //}
1647+
* ```
1648+
*/
16261649
type Repeated <: Term
16271650

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

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

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

43154341
/** A dummy class symbol that is used to indicate repeated parameters
43164342
* compiled by the Scala compiler.
4343+
*
4344+
* @see Repeated
43174345
*/
43184346
def RepeatedParamClass: Symbol
43194347

tests/pos/i18784/Macro_1.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import scala.quoted.*
2+
3+
object Macro {
4+
inline def repeated = ${Macro.repeatedImpl}
5+
def repeatedImpl(using Quotes):Expr[List[Int]] = {
6+
import quotes.reflect.*
7+
val args = List(Expr(1), Expr(2))
8+
val listObjectTerm = '{ List }.asTerm
9+
Apply(
10+
TypeApply(
11+
Select.unique(listObjectTerm, "apply"),
12+
List(TypeTree.of[Int])
13+
),
14+
List(
15+
Typed(
16+
Repeated(args.map(_.asTerm), TypeTree.of[Int]),
17+
Inferred(defn.RepeatedParamClass.typeRef.appliedTo(TypeRepr.of[Int]))))
18+
).asExprOf[List[Int]]
19+
}
20+
}

tests/pos/i18784/Test_2.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def Test: Unit =
2+
Macro.repeated

0 commit comments

Comments
 (0)