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
types scala type-conversion shapeless


source to share


No one has answered this question yet

See similar questions:

6
Using shapeless scala to combine fields of two different case classes

or similar:

3393
Create ArrayList from array
790
Is the Scala 2.8 collection library "the longest suicide record in history"?
595
Difference between object and class in Scala
492
What are the benefits of underscores in Scala?
104
IntelliJ Scala Plugin body class indentation is absurd
4
Scala / Lifeless: updating field name in case instance
3
Scala class classes and constructors
2
Derive new type from case class using Shapeless
2
Scala compares case class and gets changed fields
1
Factorization of case class attributes, regardless of the restrictions on extending the case class?



All Articles
Loading...
X
Show
Funny
Dev
Pics