jilen
jilen

Reputation: 5763

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 ?

Upvotes: 0

Views: 94

Answers (1)

Oleg Pyzhcov
Oleg Pyzhcov

Reputation: 7353

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

Upvotes: 2

Related Questions