【问题标题】:Shapeless: Why align does not work with singleton types?Shapeless:为什么 align 不适用于单例类型?
【发布时间】:2017-08-09 19:10:02
【问题描述】:

我希望这段代码能够编译

import shapeless._
import record._
import ops.record._
import ops.hlist.Align
import syntax.singleton._

case class From(i: Int, s: String, a: Int, b: Int, c: Int)
case class To(j: Int, s1: String, s2: String, a: Int, b: Int, c: Int)
val f = From(1, "FROM", 1, 1, 1)

val fromGen = LabelledGeneric[From]
val toGen = LabelledGeneric[To]

val lgenRepr = fromGen.to(f)
val modified = lgenRepr.renameField('i, 'j) - 's + ('s1 ->> "S1") + ('s2 ->> "S2")

val align = Align[modified.type, toGen.Repr]

toGen.from(align(modified))

但是如果失败了

could not find implicit value for parameter alm: shapeless.ops.hlist.Align[modified.type,toGen.Repr]
 val align = Align[modified.type, toGen.Repr]

a trick 使用labelled.FieldType 构造所需的类型,但它没有解释如何删除字段,所以如果不可能使Align 与单例类型一起工作,了解如何处理这些字段。

【问题讨论】:

    标签: scala shapeless


    【解决方案1】:

    问题在于modified.type 有点精确......它是那个特定 val 的单例类型,而不是计算的记录类型。要在 REPL 上执行此操作,以下工作正常,

    @ modified.align[toGen.Repr] 
    res14: toGen.Repr = 1 :: S1 :: S2 :: 1 :: 1 :: 1 :: HNil
    

    【讨论】:

      猜你喜欢
      • 2013-01-25
      • 2016-03-25
      • 2012-01-22
      • 2014-01-09
      • 1970-01-01
      • 2020-05-05
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      相关资源
      最近更新 更多