假设我有容器标记
case class TypedString[T](value: String)
和偏函数技巧
abstract class PartFunc[Actual <: HList] {
val poly: Poly
def apply[L1 <: HList](l: L1)(implicit
mapped: Mapped.Aux[Actual, TypedString, L1],
mapper: Mapper[poly.type, L1]): L1 = l
}
映射器的多边形
object f extends (TypedString ~>> String) {
def apply[T](s : TypedString[T]) = s.value
}
和结果方法
def func[Actual <: HList] = new PartFunc[Actual] {
val poly = f
}
使用示例:
func[
Int :: String :: HNil
](TypedString[Int]("42") :: TypedString[String]("hello") :: HNil)
此代码在编译时失败,因为编译器找不到Mapped
隐式参数:
could not find implicit value for parameter mapped:
shapeless.ops.hlist.Mapped[shapeless.::[Int,shapeless.::[String,shapeless.HNil]],nottogether.MapperTest.TypedString]{type Out = shapeless.::[nottogether.MapperTest.TypedString[Int],shapeless.::[nottogether.MapperTest.TypedString[String],shapeless.HNil]]}
](TypedString[Int]("42") :: TypedString[String]("hello") :: HNil)
但是,如果我们从签名中删除Mapper
隐式参数,一切正常。PartFunc.apply(...)
所以我不知道为什么以及如何Mapper
影响Mapped
.