我正在考虑做类似于安全地复制不同类型的案例类之间的字段但使用重新排序的字段,即
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)
如何重新排序记录中的字段(?),这样可以使用最少的样板?