gpt4 book ai didi

Scala 3 (Dotty) 模式匹配带有宏引用的函数

转载 作者:行者123 更新时间:2023-12-03 17:20:01 30 4
gpt4 key购买 nike

我正在尝试通过 Scala 3.0.0-M2 中的宏获取函数名称
我想出的解决方案使用 TreeAccumulator

import scala.quoted._

inline def getName[T](inline f: T => Any): String = ${getNameImpl('f)}

def getNameImpl[T](f: Expr[T => Any])(using Quotes): Expr[String] = {
import quotes.reflect._
val acc = new TreeAccumulator[String] {
def foldTree(names: String, tree: Tree)(owner: Symbol): String = tree match {
case Select(_, name) => name
case _ => foldOverTree(names, tree)(owner)
}
}
val fieldName = acc.foldTree(null, Term.of(f))(Symbol.spliceOwner)
Expr(fieldName)
}
调用此代码时会生成函数的名称:
case class B(field1: String)
println(getName[B](_.field1)) // "field1"
我想知道这是否可以使用引号以更简单的方式完成。

最佳答案

我想这足以定义

def getNameImpl[T: Type](f: Expr[T => Any])(using Quotes): Expr[String] = {
import quotes.reflect._
Expr(TypeTree.of[T].symbol.caseFields.head.name)
}
实际上,我不使用 f .
测试:
println(getName[B](_.field1)) // "field1"
在 3.0.0-M3-bin-20201211-dbc1186-NIGHTLY 中测试。
How to access parameter list of case class in a dotty macro
或者你可以试试
def getNameImpl[T](f: Expr[T => Any])(using Quotes): Expr[String] = {
import quotes.reflect._

val fieldName = f.asTerm match {
case Inlined(
_,
List(),
Block(
List(DefDef(
_,
List(),
List(List(ValDef(_, _, _))),
_,
Some(Select(Ident(_), fn))
)),
Closure(_, _)
)
) => fn
}

Expr(fieldName)
}

关于Scala 3 (Dotty) 模式匹配带有宏引用的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65189756/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com