@@ -525,6 +525,8 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
525525 * `Some` containing the implementation of the method. Returns `None` the method has no implementation.
526526 * Any definition directly inside the implementation should have `symbol` as owner.
527527 *
528+ * Use `Symbol.asQuotes` to create the rhs using quoted code.
529+ *
528530 * See also: `Tree.changeOwner`
529531 */
530532 def apply (symbol : Symbol , rhsFn : List [List [Tree ]] => Option [Term ]): DefDef
@@ -602,20 +604,53 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
602604 * Returns `None` the method has no implementation.
603605 * Any definition directly inside the implementation should have `symbol` as owner.
604606 *
607+ * Use `Symbol.asQuotes` to create the rhs using quoted code.
608+ *
605609 * See also: `Tree.changeOwner`
606610 */
607611 def apply (symbol : Symbol , rhs : Option [Term ]): ValDef
608612 def copy (original : Tree )(name : String , tpt : TypeTree , rhs : Option [Term ]): ValDef
609613 def unapply (vdef : ValDef ): (String , TypeTree , Option [Term ])
610614
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+ */
612627 def let (owner : Symbol , name : String , rhs : Term )(body : Ref => Term ): Term
613628
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+ */
615641 def let (owner : Symbol , rhs : Term )(body : Ref => Term ): Term =
616642 let(owner, " x" , rhs)(body)
617643
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+ */
619654 def let (owner : Symbol , terms : List [Term ])(body : List [Ref ] => Term ): Term
620655 }
621656
@@ -1341,6 +1376,28 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
13411376 * ```scala sc:nocompile
13421377 * Block((DefDef(_, _, params :: Nil, _, Some(rhsFn(meth, paramRefs)))) :: Nil, Closure(meth, _))
13431378 * ```
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+ *
13441401 * @param owner: owner of the generated `meth` symbol
13451402 * @param tpe: Type of the definition
13461403 * @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 =>
38173874
38183875 /** Case class or case object children of a sealed trait or cases of an `enum`. */
38193876 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+
38203906 end extension
38213907 }
38223908
@@ -4497,6 +4583,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
44974583 * override def transformTree(tree: Tree)(owner: Symbol): Tree = ???
44984584 * }
44994585 * ```
4586+ *
4587+ * Use `Symbol.asQuotes` to create quotes with the correct owner within the TreeMap.
4588+ *
45004589 * @syntax markdown
45014590 */
45024591 trait TreeMap :
0 commit comments