@@ -525,6 +525,8 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
525
525
* `Some` containing the implementation of the method. Returns `None` the method has no implementation.
526
526
* Any definition directly inside the implementation should have `symbol` as owner.
527
527
*
528
+ * Use `Symbol.asQuotes` to create the rhs using quoted code.
529
+ *
528
530
* See also: `Tree.changeOwner`
529
531
*/
530
532
def apply (symbol : Symbol , rhsFn : List [List [Tree ]] => Option [Term ]): DefDef
@@ -602,20 +604,53 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
602
604
* Returns `None` the method has no implementation.
603
605
* Any definition directly inside the implementation should have `symbol` as owner.
604
606
*
607
+ * Use `Symbol.asQuotes` to create the rhs using quoted code.
608
+ *
605
609
* See also: `Tree.changeOwner`
606
610
*/
607
611
def apply (symbol : Symbol , rhs : Option [Term ]): ValDef
608
612
def copy (original : Tree )(name : String , tpt : TypeTree , rhs : Option [Term ]): ValDef
609
613
def unapply (vdef : ValDef ): (String , TypeTree , Option [Term ])
610
614
611
- /** Creates a block `{ val <name> = <rhs: Term>; <body(x): Term> }` */
615
+ /** Creates a block `{ val <name> = <rhs: Term>; <body(x): Term> }`
616
+ *
617
+ * Usage:
618
+ * ```
619
+ * ValDef.let(owner, "x", rhs1) { x =>
620
+ * ValDef.let(x.symbol.owner, "y", rhs2) { y =>
621
+ * // use `x` and `y`
622
+ * }
623
+ * }
624
+ * ```
625
+ * @syntax markdown
626
+ */
612
627
def let (owner : Symbol , name : String , rhs : Term )(body : Ref => Term ): Term
613
628
614
- /** Creates a block `{ val x = <rhs: Term>; <body(x): Term> }` */
629
+ /** Creates a block `{ val x = <rhs: Term>; <body(x): Term> }`
630
+ *
631
+ * Usage:
632
+ * ```
633
+ * ValDef.let(owner, rhs1) { x =>
634
+ * ValDef.let(owner, rhs2) { y =>
635
+ * // use `x` and `y`
636
+ * }
637
+ * }
638
+ * ```
639
+ * @syntax markdown
640
+ */
615
641
def let (owner : Symbol , rhs : Term )(body : Ref => Term ): Term =
616
642
let(owner, " x" , rhs)(body)
617
643
618
- /** Creates a block `{ val x1 = <terms(0): Term>; ...; val xn = <terms(n-1): Term>; <body(List(x1, ..., xn)): Term> }` */
644
+ /** Creates a block `{ val x1 = <terms(0): Term>; ...; val xn = <terms(n-1): Term>; <body(List(x1, ..., xn)): Term> }`
645
+ *
646
+ * Usage:
647
+ * ```
648
+ * ValDef.let(owner, rhsList) { xs =>
649
+ * ...
650
+ * }
651
+ * ```
652
+ * @syntax markdown
653
+ */
619
654
def let (owner : Symbol , terms : List [Term ])(body : List [Ref ] => Term ): Term
620
655
}
621
656
@@ -1341,6 +1376,28 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
1341
1376
* ```scala sc:nocompile
1342
1377
* Block((DefDef(_, _, params :: Nil, _, Some(rhsFn(meth, paramRefs)))) :: Nil, Closure(meth, _))
1343
1378
* ```
1379
+ *
1380
+ * Usage:
1381
+ * ```
1382
+ * val mtpe = MethodType(List("arg1"))(_ => List(TypeRepr.of[Int]), _ => TypeRepr.of[Int])
1383
+ * Lambda(owner, mtpe, {
1384
+ * case (methSym, List(arg1: Term)) =>
1385
+ * ValDef.let(methSym, f(arg1)) { ... }
1386
+ * }
1387
+ * )
1388
+ * ```
1389
+ *
1390
+ * Usage with quotes:
1391
+ * ```
1392
+ * val mtpe = MethodType(List("arg1"))(_ => List(TypeRepr.of[Int]), _ => TypeRepr.of[Int])
1393
+ * Lambda(owner, mtpe, {
1394
+ * case (methSym, List(arg1: Term)) =>
1395
+ * given Quotes = methSym.asQuotes
1396
+ * '{ ... }
1397
+ * }
1398
+ * )
1399
+ * ```
1400
+ *
1344
1401
* @param owner: owner of the generated `meth` symbol
1345
1402
* @param tpe: Type of the definition
1346
1403
* @param rhsFn: Function that receives the `meth` symbol and the a list of references to the `params`
@@ -3817,6 +3874,35 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3817
3874
3818
3875
/** Case class or case object children of a sealed trait or cases of an `enum`. */
3819
3876
def children : List [Symbol ]
3877
+
3878
+ /** Returns a nested quote with this symbol as splice owner (`Symbol.spliceOwner`).
3879
+ *
3880
+ * Changes the owner under which the definition in a quote are created.
3881
+ *
3882
+ * Usages:
3883
+ * ```scala
3884
+ * def rhsExpr(using Quotes): Expr[Unit] = '{ val y = ???; (y, y) }
3885
+ * def aValDef(using Quotes)(owner: Symbol) =
3886
+ * val sym = Symbol.newVal(owner, "x", TypeRepr.of[Unit], Flags.EmptyFlags, Symbol.noSymbol)
3887
+ * val rhs = rhsExpr(using sym.asQuotes).asTerm
3888
+ * ValDef(sym, Some(rhs))
3889
+ * ```
3890
+ *
3891
+ * ```scala
3892
+ * new TreeMap:
3893
+ * override def transformTerm(tree: Term)(owner: Symbol): Term =
3894
+ * tree match
3895
+ * case tree: Ident =>
3896
+ * given Quotes = owner.asQuotes
3897
+ * // Definitions contained in the quote will be owned by `owner`.
3898
+ * // No need to use `changeOwner` in this case.
3899
+ * '{ val x = ???; x }.asTerm
3900
+ * ```
3901
+ * @syntax markdown
3902
+ */
3903
+ @ experimental
3904
+ def asQuotes : Nested
3905
+
3820
3906
end extension
3821
3907
}
3822
3908
@@ -4497,6 +4583,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
4497
4583
* override def transformTree(tree: Tree)(owner: Symbol): Tree = ???
4498
4584
* }
4499
4585
* ```
4586
+ *
4587
+ * Use `Symbol.asQuotes` to create quotes with the correct owner within the TreeMap.
4588
+ *
4500
4589
* @syntax markdown
4501
4590
*/
4502
4591
trait TreeMap :
0 commit comments