我有性状标记
trait TypedTrait {
type TYPE
}
和实现
case class TypedString[U](value: String) extends TypedTrait {
type TYPE = U
}
根据TypedString
的类型参数,将String
的HList
映射为TypedString
的HList
。
最简单的方法是创建convert
方法(根据目标类型在Shapeless map HList中描述):
val list = "Hello" :: "world" :: HNil
val mapped: TypedString[Int] :: TypedString[Boolean] :: HNil =
convert[TypedString[Int] :: TypedString[Boolean] :: HNil](list)
但是我想避免多余的参数化,并使用这样的东西:
val mapped: TypedString[Int] :: TypedString[Boolean] :: HNil =
convert[Int :: Boolean :: HNil](list)
第一个解决方案的完整代码示例:
import shapeless._
trait TypedTrait {
type TYPE
}
case class TypedString[U](value: String) extends TypedTrait {
type TYPE = U
}
trait Convert[I <: HList, O <: HList] { def apply(i: I): O }
object Convert extends LowPriorityConvertInstances {
implicit val convertHNil: Convert[HNil, HNil] = new Convert[HNil, HNil] {
def apply(i: HNil): HNil = i
}
implicit def convertHConsTS[TS, T <: HList, TO <: HList](implicit
c: Convert[T, TO]
): Convert[String :: T, TypedString[TS] :: TO] =
new Convert[String :: T, TypedString[TS] :: TO] {
def apply(i: String :: T): TypedString[TS] :: TO = TypedString[TS](i.head) :: c(i.tail)
}
}
sealed class LowPriorityConvertInstances {
implicit def convertHCons[H, T <: HList, TO <: HList](implicit
c: Convert[T, TO]
): Convert[H :: T, H :: TO] = new Convert[H :: T, H :: TO] {
def apply(i: H :: T): H :: TO = i.head :: c(i.tail)
}
}
class PartiallyAppliedConvert[O <: HList] {
def apply[I <: HList](i: I)(implicit c: Convert[I, O]): O = c(i)
}
def convert[O <: HList]: PartiallyAppliedConvert[O] =
new PartiallyAppliedConvert[O]
val list = "Hello" :: "world" :: HNil
val mapped: TypedString[Int] :: TypedString[String] :: HNil =
convert[TypedString[Int] :: TypedString[String] :: HNil](list)
您可以通过在Convert
中使用三个HList
类型的参数来实现这一点:
- 传递给
convert
的实际HList
的类型(例如,String :: String :: HNil
) - 用户指定的类型参数(如
Int :: Boolean :: HNil
) - 输出类型-基本上是规定的
HList
包裹在TypedString
:例如,TypedString[Int] :: TypedString[Boolean] :: HNil
。
输出类型可以完全从规定的HList
计算出来,因此我将使用shapeless
代码通常使用的Aux
模式:
trait Convert[In <: HList, Prescribed <: HList] {
type Out <: HList
def apply(i: In): Out
}
object Convert {
type Aux[I <: HList, P <: HList, O <: HList] = Convert[I, P] { type Out = O }
// Adapt the implicits accordingly.
// The low priority one is left as an exercise to the reader.
implicit val convertHNil: Convert.Aux[HNil, HNil, HNil] =
new Convert[HNil, HNil] {
type Out = HNil
def apply(i: HNil): HNil = i
}
implicit def convertHConsTS[TS, TI <: HList, TP <: HList, TO <: HList](implicit
c: Convert.Aux[TI, TP, TO]
): Convert.Aux[String :: TI, TS :: TP, TypedString[TS] :: TO] =
new Convert[String :: TI, TS :: TP] {
type Out = TypedString[TS] :: TO
def apply(i: String :: TI): TypedString[TS] :: TO =
TypedString[TS](i.head) :: c(i.tail)
}
}
class PartiallyAppliedConvert[P <: HList] {
def apply[I <: HList](i: I)(implicit c: Convert[I, P]): c.Out = c(i)
}
def convert[O <: HList]: PartiallyAppliedConvert[O] =
new PartiallyAppliedConvert[O]
val list = "Hello" :: "world" :: HNil
val mapped = convert[Int :: String :: HNil](list)
结果:scala> mapped
res3: shapeless.::[com.Main.TypedString[Int],shapeless.::[com.Main.TypedString[String],shapeless.HNil]] = TypedString(Hello) :: TypedString(world) :: HNil
我相信可以使用shapeless
提供的一些操作来实现这一点(shapeless.ops.hlist.Mapped
, shapeless.ops.hlist.HKernel
或shapeless.ops.hlist.RightFolder
看起来合适),但我不知道如何编写Poly
函数,它接受类型参数和正常参数。欢迎大家多多指教。