【发布时间】:2020-12-09 04:22:18
【问题描述】:
我知道 HList 的类型,我想将特定函数应用于 HList 中的每个特定项。例如。
我知道 HList 的类型,在本例中为 String :: Int :: String :: HNil
val values = "Hello World" :: 5 :: "Goodbye World" :: HNil
所以我从 type => Int 为每个项目创建一个函数。
val funcs =
((_: String) => 1) ::
((_: Int) => 2) ::
((_: String) => 3) ::
HNil
我想对列表中的每个项目应用函数 1 到值 1,函数 2 到值 2,等等。这是对函数签名的最佳猜测。 V
// V are the Values of type String :: Int :: String :: HNil
// F are the Functions of type (String => Int) :: (Int => Int) :: (String => Int) :: HNil
// R is the Int Results of type Int :: Int :: Int :: HNil
def applyFunction[V <: HList, F <: HList, R <: HList](v: V, f: F)(
implicit ev: V =:= F,
utcc: UnaryTCConstraint[F, Function[*, Int]]
): R
现在我正在为如何实际实现这个功能而苦苦挣扎,或者也许 Shapeless 已经有了可以做到这一点的东西。
【问题讨论】: