首页 > 解决方案 > 如何在scala 3宏中匹配函数Expr

问题描述

使用 scala 3 引号语法:

'{...}

如何匹配函数表达式?

def macroImpl(expr: Expr[T => Any])(using q: Quotes): Expr[Any] = {
 expr match {
   case '{/*how to match function here*/} => ???
 }
}

标签: scalametaprogrammingscala-macrosscala-3

解决方案


一种方法是:

def macroImpl[T : Type](expr: Expr[T => Any])(using q: Quotes): Expr[Any] = {  
    expr match {
      case '{(x : T) => ${body} : Any } => .... //do something
    }
}

推荐阅读