Shapeless - turn a case class into another with fields in different order

前端 未结 2 1387
悲哀的现实
悲哀的现实 2020-12-07 21:00

I\'m thinking of doing something similar to Safely copying fields between case classes of different types but with reordered fields, i.e.

case class A(foo: I         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-07 21:44

    As noticed @MilesSabin (godlike shapeless creator), there is an align operation, it is used like:

    import ops.hlist.Align
    
    val aGen = LabelledGeneric[A]
    val bGen = LabelledGeneric[B]
    val align = Align[aGen.Repr, bGen.Repr]
    bGen.from(align(aGen.to(A(12, 13)))) //> res0: B = B(13,12)
    

    P.S. Noticed that there is an example on GitHub.

提交回复
热议问题