0

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 ?

jilen
  • 5,633
  • 3
  • 35
  • 84
  • 1
    why not use `tupled` like this `def transpose(func: Func): HFunc = (t: HListType) => func.tupled(t.tupled)` – 余杰水 Feb 28 '18 at 05:34

1 Answers1

2

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
Oleg Pyzhcov
  • 7,323
  • 1
  • 18
  • 30