Skip to content
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

Update varargs syntax in quote pattern examples #2980

Merged
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
10 changes: 5 additions & 5 deletions _overviews/scala3-macros/tutorial/quotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ given ToExpr[Boolean] with {
given ToExpr[StringContext] with {
def apply(stringContext: StringContext)(using Quotes) =
val parts = Varargs(stringContext.parts.map(Expr(_)))
'{ StringContext($parts: _*) }
'{ StringContext($parts*) }
}
```
The `Varargs` constructor just creates an `Expr[Seq[T]]` which we can efficiently splice as a varargs.
In general, any sequence can be spliced with `$mySeq: _*` to splice it as a varargs.
In general, any sequence can be spliced with `$mySeq*` to splice it as a varargs.

## Quoted patterns
Quotes can also be used to check if an expression is equivalent to another or to deconstruct an expression into its parts.
Expand Down Expand Up @@ -352,7 +352,7 @@ Types represented with `Type[T]` can be matched on using the patten `case '[...]
inline def mirrorFields[T]: List[String] = ${mirrorFieldsImpl[T]}

def mirrorFieldsImpl[T: Type](using Quotes): Expr[List[String]] =

def rec[A : Type]: List[String] = Type.of[A] match
case '[field *: fields] =>
Type.show[field] :: rec[fields]
Expand Down Expand Up @@ -408,8 +408,8 @@ given FromExpr[Boolean] with {

given FromExpr[StringContext] with {
def unapply(x: Expr[StringContext])(using Quotes): Option[StringContext] = x match {
case '{ new StringContext(${Varargs(Exprs(args))}: _*) } => Some(StringContext(args: _*))
case '{ StringContext(${Varargs(Exprs(args))}: _*) } => Some(StringContext(args: _*))
case '{ new StringContext(${Varargs(Exprs(args))}*) } => Some(StringContext(args*))
case '{ StringContext(${Varargs(Exprs(args))}*) } => Some(StringContext(args*))
case _ => None
}
}
Expand Down
Loading