[英]scala macro how to convert `HList` to function args
对于以下类型
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
?
如果您只想进行转换(不一定是宏),那么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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.