[英]Shapeless HList map after foldRight
A type level foldRight
works fine (getLabelWithValues), and a follow-on type level map
(getValues) also works fine. 类型级别
foldRight
工作正常(getLabelWithValues),后续类型级别map
(getValues)也可以正常工作。 If I combine both in one method (getValuesFull), it doesn't work any more though. 如果我在一个方法(getValuesFull)中组合两者,它就不再起作用了。 What is the missing piece?
丢失的是什么?
The full source (with sbt ready to ~run
with implicit debug output) is here: https://github.com/mpollmeier/shapeless-playground/tree/8170a5b 完整的源代码(sbt准备
~run
隐式调试输出)在这里: https : //github.com/mpollmeier/shapeless-playground/tree/8170a5b
case class Label[A](name: String)
case class LabelWithValue[A](label: Label[A], value: A)
val label1 = Label[Int]("a")
val labels = label1 :: HNil
object combineLabelWithValue extends Poly2 {
implicit def atLabel[A, B <: HList] = at[Label[A], (B, Map[String, Any])] {
case (label, (acc, values)) ⇒
(LabelWithValue(label, values(label.name).asInstanceOf[A]) :: acc, values)
}
}
object GetLabelValue extends (LabelWithValue ~> Id) {
def apply[B](labelWithValue: LabelWithValue[B]) = labelWithValue.value
}
val labelsWithValues: LabelWithValue[Int] :: HNil = getLabelWithValues(labels)
// manually mapping it works fine:
val valuesManual: Int :: HNil = labelsWithValues.map(GetLabelValue)
// using a second function with Mapper works fine:
val valuesSecondFn: Int :: HNil = getValues(labelsWithValues)
// error: could not find implicit value for parameter mapper: shapeless.ops.hlist.Mapper.Aux[Main.GetLabelValue.type,WithValues,Values]
// val valuesFull: Int :: HNil = getValuesFull(labels)
def getLabelWithValues[L <: HList, P, WithValues](labels: L)(
implicit folder: RightFolder.Aux[L, (HNil.type, Map[String, Any]), combineLabelWithValue.type, P],
ic: IsComposite.Aux[P, WithValues, _]
): WithValues = {
val state = Map("a" -> 5, "b" -> "five")
val resultTuple = labels.foldRight((HNil, state))(combineLabelWithValue)
ic.head(resultTuple)
}
def getValues[WithValues <: HList, Values <: HList](withValues: WithValues)(
implicit mapper: Mapper.Aux[GetLabelValue.type, WithValues, Values]
): Values = {
withValues.map(GetLabelValue)
}
def getValuesFull[L <: HList, P, WithValues <: HList, Values <: HList](labels: L)(
implicit folder: RightFolder.Aux[L, (HNil.type, Map[String, Any]), combineLabelWithValue.type, P],
ic: IsComposite.Aux[P, WithValues, _],
mapper: Mapper.Aux[GetLabelValue.type, WithValues, Values]
): Values = {
val state = Map("a" -> 5, "b" -> "five")
val resultTuple = labels.foldRight((HNil, state))(combineLabelWithValue)
val withValues: WithValues = ic.head(resultTuple)
withValues.map(GetLabelValue)
}
The issue here is that you're ending up trying to map over an HList
where the HNil
is statically typed as HNil.type
. 这里的问题是你最终试图映射
HList
,其中HNil
被静态类型化为HNil.type
。 This doesn't work in general—eg in a simplified case like this: 这通常不起作用 - 例如在这样的简化情况下:
import shapeless._, ops.hlist.Mapper
val mapped1 = Mapper[poly.identity.type, HNil]
val mapped2 = Mapper[poly.identity.type, HNil.type]
mapped1
will compile, but mapped2
won't. mapped1
将编译,但mapped2
不会。
The trick is to change the HNil.type
in your RightFolder
types to HNil
and then to call foldRight
with HNil: HNil
. 关键是要改变
HNil.type
在RightFolder
类型HNil
,然后调用foldRight
与HNil: HNil
。 This will make everything work just fine. 这将使一切工作正常。
There are a few other suggestions I'd make (destructure the tuple in place of P
instead of using IsComposite
, skip the Aux
on mapper
and return mapper.Out
instead of having a Value
type parameter, etc.), but they're probably out of the scope of this question. 还有一些其他的建议(
IsComposite
元组代替P
而不是使用IsComposite
,跳过mapper
上的Aux
并返回mapper.Out
而不是具有Value
类型参数等),但它们可能是超出了这个问题的范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.