简体   繁体   中英

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

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 ?

If all you want is the conversion (and not necessarily macro), shapeless provides these out of the box as extension methods on functions:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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