以下类型
type HFunc = (Int :: String :: HNil) => Int
type Func = (Int, String) => Int
我尝试将Func
转换为HFunc
val funExpr: Tree = ???
val hlistType = ???
val hfuncName = c.freshName("hfunc")
q"""
def $hfuncName(t: $hlistType) = {
${funExpr}(..) //how to extract hlist elements as params ?
}
"""
如何提取HList
元素并将其传递给Func
?
答案 0 :(得分:2)
如果你想要的只是转换(而不一定是宏),那么shapeless提供了开箱即用的功能扩展方法:
import shapeless._
import shapeless.syntax.std.function._
type HFunc = (Int :: String :: HNil) => Int
type Func = (Int, String) => Int
def toHFunc(f: Func): HFunc = f.toProduct
def fromHFunc(hf: HFunc): Func = hf.fromProduct