scala宏如何将`HList`转换为函数参数

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