Skip to content

Commit 34d234f

Browse files
committed
Add variant of let with custom name
1 parent 3b01a9f commit 34d234f

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -707,8 +707,10 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
707707
def unapply(x: Block): Option[(List[Statement], Term)] =
708708
Some((x.statements, x.expr))
709709

710-
def let(rhs: Term)(body: Ident => Term): Term =
711-
let(List(rhs))(x => body(x.head))
710+
def let(name: String, rhs: Term)(body: Ident => Term): Term =
711+
val vdef = tpd.SyntheticValDef(name.toTermName, rhs)
712+
val ref = tpd.ref(vdef.symbol).asInstanceOf[Ident]
713+
Block(List(vdef), body(ref))
712714

713715
def let(terms: List[Term])(body: List[Ident] => Term): Term =
714716
val vdefs = terms.map(term => tpd.SyntheticValDef("x".toTermName, term))

library/src/scala/tasty/Reflection.scala

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -818,16 +818,19 @@ trait Reflection { reflection =>
818818
/** Creates a block `{ <statements: List[Statement]>; <expr: Term> }` */
819819
def apply(stats: List[Statement], expr: Term): Block
820820

821-
/** Creates a block `{ val x = <rhs: Term>; <body(x): Term> }` */
822-
def let(rhs: Term)(body: Ident => Term): Term
823-
824-
/** Creates a block `{ val x1 = <terms(0): Term>; ...; val xn = <terms(n-1): Term>; <body(List(x1, ..., xn)): Term> }` */
825-
def let(terms: List[Term])(body: List[Ident] => Term): Term
826-
827821
def copy(original: Tree)(stats: List[Statement], expr: Term): Block
828822

829823
/** Matches a block `{ <statements: List[Statement]>; <expr: Term> }` */
830824
def unapply(x: Block): Option[(List[Statement], Term)]
825+
826+
/** Creates a block `{ val <name> = <rhs: Term>; <body(x): Term> }` */
827+
def let(name: String, rhs: Term)(body: Ident => Term): Term
828+
829+
/** Creates a block `{ val x = <rhs: Term>; <body(x): Term> }` */
830+
def let(rhs: Term)(body: Ident => Term): Term = let("x", rhs)(body)
831+
832+
/** Creates a block `{ val x1 = <terms(0): Term>; ...; val xn = <terms(n-1): Term>; <body(List(x1, ..., xn)): Term> }` */
833+
def let(terms: List[Term])(body: List[Ident] => Term): Term
831834
}
832835

833836
given BlockMethods as BlockMethods = BlockMethodsImpl

0 commit comments

Comments
 (0)