相关疑难解决方法(0)

无形 - 将一个案例类转换为另一个不同顺序的字段

我正在考虑做类似于安全地复制不同类型的案例类之间的字段但使用重新排序的字段,即

case class A(foo: Int, bar: Int)
case class B(bar: Int, foo: Int)
Run Code Online (Sandbox Code Playgroud)

我想有东西转A(3, 4)B(4, 3)-不成形LabelledGeneric浮现在脑海,但是

LabelledGeneric[B].from(LabelledGeneric[A].to(A(12, 13)))
Run Code Online (Sandbox Code Playgroud)

结果是

<console>:15: error: type mismatch;
 found   : shapeless.::[shapeless.record.FieldType[shapeless.tag.@@[Symbol,String("foo")],Int],shapeless.::[shapeless.record.FieldType[shapeless.tag.@@[Symbol,String("bar")],Int],shapeless.HNil]]
    (which expands to)  shapeless.::[Int with shapeless.record.KeyTag[Symbol with shapeless.tag.Tagged[String("foo")],Int],shapeless.::[Int with shapeless.record.KeyTag[Symbol with shapeless.tag.Tagged[String("bar")],Int],shapeless.HNil]]
 required: shapeless.::[shapeless.record.FieldType[shapeless.tag.@@[Symbol,String("bar")],Int],shapeless.::[shapeless.record.FieldType[shapeless.tag.@@[Symbol,String("foo")],Int],shapeless.HNil]]
    (which expands to)  shapeless.::[Int with shapeless.record.KeyTag[Symbol with shapeless.tag.Tagged[String("bar")],Int],shapeless.::[Int with shapeless.record.KeyTag[Symbol with shapeless.tag.Tagged[String("foo")],Int],shapeless.HNil]]
              LabelledGeneric[B].from(LabelledGeneric[A].to(A(12, 13)))
                                                           ^
Run Code Online (Sandbox Code Playgroud)

如何重新排序记录中的字段(?),这样可以使用最少的样板?

scala shapeless labelled-generic

21
推荐指数
2
解决办法
2694
查看次数

标签 统计

labelled-generic ×1

scala ×1

shapeless ×1