[英]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.