获取Scala 3宏中的封闭模块



如何获得宏调用的封闭模块

package foo.bar
class MyClass:
val macroReturnValue = SomeMacro.macroCall("someExpression")
end MyClass

macroReturnValue应包含例如"foo.bar.MyClass"

Try

import scala.quoted.*
object SomeMacro:
inline def macroCall(s: String): String = ${macroCallImpl('s)}
def macroCallImpl(s: Expr[String])(using Quotes): Expr[String] =
import quotes.reflect.*
def enclosingClass(symb: Symbol): Symbol = 
if symb.isClassDef then symb else enclosingClass(symb.owner)
val name = enclosingClass(Symbol.spliceOwner).fullName
Literal(StringConstant(name)).asExprOf[String]

相关内容

  • 没有找到相关文章

最新更新