简体   繁体   English

Scala宏如何将`HList`转换为函数args

[英]scala macro how to convert `HList` to function args

For below types 对于以下类型

type HFunc = (Int :: String :: HNil) => Int

type Func = (Int, String) => Int

I try to convert Func to HFunc 我尝试将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 ?
  }
"""

How Can I extract the HList elements and pass it to the Func ? 如何提取HList元素并将其传递给Func

If all you want is the conversion (and not necessarily macro), shapeless provides these out of the box as extension methods on functions: 如果您只想进行转换(不一定是宏),那么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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM