|
| 1 | +//> using options -experimental |
| 2 | +import scala.quoted._ |
| 3 | +class Foo { type test1; val test2: List[Any] = List() } |
| 4 | +object Macro: |
| 5 | + inline def inlineCall(): Any = ${impl} |
| 6 | + transparent inline def transparentInlineCall(): Any = ${impl} |
| 7 | + |
| 8 | + def impl(using Quotes): Expr[Any] = |
| 9 | + import quotes.reflect._ |
| 10 | + val tpt = TypeTree.of[Foo] |
| 11 | + |
| 12 | + val typeDefSym = Symbol.newTypeAlias(Symbol.spliceOwner, "test1", Flags.EmptyFlags, TypeRepr.of[Int], Symbol.noSymbol) |
| 13 | + val valDefSym = Symbol.newVal(Symbol.spliceOwner, "test2", TypeRepr.of[List[Int]], Flags.EmptyFlags, Symbol.noSymbol) |
| 14 | + |
| 15 | + val defs = List( |
| 16 | + TypeDef(typeDefSym), |
| 17 | + ValDef(valDefSym, None) |
| 18 | + ) |
| 19 | + val typeTree = Refined(tpt, defs, TypeRepr.of[Foo].typeSymbol) |
| 20 | + |
| 21 | + // A little redundant, but we only want to see if Refined.apply() works, |
| 22 | + // so we only replace the type with one we just constructed |
| 23 | + val body = '{ |
| 24 | + new Foo { type test1 = Int; override val test2: List[Int] = List(0) } |
| 25 | + } |
| 26 | + val blockWithNewTypeTree = body.asTerm match |
| 27 | + case Inlined(_, _, Block(cDef : ClassDef, Typed(invocation, _))) => |
| 28 | + Block(cDef, Typed(invocation, typeTree)) |
| 29 | + |
| 30 | + blockWithNewTypeTree.asExpr |
0 commit comments