Skip to content

Commit 3cc71be

Browse files
Add missing version of ValDef.let which also accepts flags
1 parent 1b5138d commit 3cc71be

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,11 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
369369
def unapply(vdef: ValDef): (String, TypeTree, Option[Term]) =
370370
(vdef.name.toString, vdef.tpt, optional(vdef.rhs))
371371

372+
def let(owner: Symbol, name: String, rhs: Term, flags: Flags)(body: Ref => Term): Term =
373+
val vdef = tpd.SyntheticValDef(name.toTermName, rhs, flags)(using ctx.withOwner(owner))
374+
val ref = tpd.ref(vdef.symbol).asInstanceOf[Ref]
375+
Block(List(vdef), body(ref))
376+
372377
def let(owner: Symbol, name: String, rhs: Term)(body: Ref => Term): Term =
373378
val vdef = tpd.SyntheticValDef(name.toTermName, rhs)(using ctx.withOwner(owner))
374379
val ref = tpd.ref(vdef.symbol).asInstanceOf[Ref]

library/src/scala/quoted/Quotes.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,22 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
682682
def copy(original: Tree)(name: String, tpt: TypeTree, rhs: Option[Term]): ValDef
683683
def unapply(vdef: ValDef): (String, TypeTree, Option[Term])
684684

685+
/** Creates a block `{ val <name> = <rhs: Term>; <body(x): Term> }`
686+
*
687+
* Usage:
688+
* ```
689+
* ValDef.let(owner, "x", rhs1, Flags.Lazy) { x =>
690+
* ValDef.let(x.symbol.owner, "y", rhs2, Flags.Mutable) { y =>
691+
* // use `x` and `y`
692+
* }
693+
* }
694+
* ```
695+
*
696+
* @param flags extra flags to with which the symbol should be constructed. Can be `Private | Protected | Override | Deferred | Final | Param | Implicit | Lazy | Mutable | Local | ParamAccessor | Module | Package | Case | CaseAccessor | Given | Enum | JavaStatic | Synthetic | Artifact`
697+
*/
698+
// Keep: `flags` doc aligned with QuotesImpl's `validValFlags`
699+
def let(owner: Symbol, name: String, rhs: Term, flags: Flags)(body: Ref => Term): Term
700+
685701
/** Creates a block `{ val <name> = <rhs: Term>; <body(x): Term> }`
686702
*
687703
* Usage:

0 commit comments

Comments
 (0)