Scala - create case class with all fields from 2 other case classes

Let's say I have 2 case classes:

case class Basic(id: String, name) extends SomeBasicTrait
case class Info (age: Int, country: String, many other fields..) extends SomeInfoTrait

      

and want to create a case class that has all fields from both classes. This is possible:

case class Full(bs: Basic, meta: Info) extends SomeBasicTrait with SomeInfoTrait {
 val id = bs.id
 val name = bs.name
 val age = meta.age
 val country = meta.country
 // etc
} 

      

But that's a lot of templates. Is there a way to avoid this?

I couldn't find a way to achieve this in Shapeless, but maybe there is ..

[ Refresh ]

@jamborta's comment helps and basically this:

case class FullTwo(id: String, name: String, age:Int, country:String)

val b = Basic("myid", "byname")
val i = Info(12, "PT")
Generic[FullTwo].from(Generic[Basic].to(b) ++ Generic[Info].to(i))

      

The problem with this solution is that we still need to define each field in the class arguments FullTwo

, so every time a change to Basic

or occurs Info

, we also have to be aware of the change FullTwo

.

Is there a way to dynamically create a case class equal at compile time FullTwo

?

+3


source to share





All Articles