簡體   English   中英

Scala宏如何將`HList`轉換為函數args

[英]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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM