Macro-Generated Field Referencing #13073
-
With Scala 2 macros, it was possible to take an arbitrary string and use it as the name of a field to reference: val term = TermName("foo")
q"obj.$term" Is this possible with the reflection API in Scala 3 macros? I feel like |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I don't know if there is a way to splice these into quasiquotes. When working with reflect trees, a |
Beta Was this translation helpful? Give feedback.
I don't know if there is a way to splice these into quasiquotes. When working with reflect trees, a
Term
can represent an instance of a class, and aSymbol
can represent a method or field name. If you have aTerm
representing an object (probably fromExpr#asTerm
) and aSymbol
representing the field, you can create a tree that calls the field on the object by creating aSelect
tree, either withTerm#select
or one of theSelectModule
methods. Then theSelect
tree can be turned back into anExpr
withTerm#asExprOf
.