Is it possible to convert between two case class with different name and slightly different field in scala

ustcyue

for example, I have case classes

case class foo(timestamp:String, epochSecond: Long)

and

case class bar(epochSecond: Long, timestamp:String)

Is it possible to make conversion between the instance of these two classes?

jwvh

Your question is rather vague because there are so many ways to accomplish this. Here's one option.

With the definition of the case classes ...

case class Foo(timestamp:String, epochSecond: Long)
case class Bar(epochSecond: Long, timestamp:String)

... add auxiliary factory methods.

object Foo {
  def apply(b: Bar): Foo = this(b.timestamp, b.epochSecond)
}
object Bar {
  def apply(f: Foo): Bar = this(f.epochSecond, f.timestamp)
}

Now you can transition from one type to the other very simply.

val fooX = Foo("noon", 42L)
val barY = Bar(99L, "none")

val fooY = Foo(barY)  //fooY: Foo = Foo(none,99)
val barX = Bar(fooX)  //barX: Bar = Bar(42,noon)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is it possible to extract same field with two different name through regex?

From Dev

Scala Case Class with Different Argument Types

From Dev

Deserialize two slightly different JSON strings (same structure, different names) to the same class

From Dev

Differentiate between two strings with different case in mysql

From Dev

why does a scala class in the worksheet with same name but different case as the worksheet cause an exception?

From Dev

is it possible to calculate data between two different cubes?

From Dev

Is it possible to ssh between two different network?

From Dev

Same use case, different actors, slightly different scenarios

From Dev

Pascal - Class inheritance between two different files?

From Dev

Using shapeless scala to merge the fields of two different case classes

From Dev

possible unique combinations between between two arrays(of different size) in python?

From Dev

possible unique combinations between between two arrays(of different size) in python?

From Dev

scala/spark different case class based on input data

From Dev

Scala case class whose fields can be mandatory and optional at different instances

From Dev

Git remote two branches same name different case

From Dev

Extract scala case class field

From Dev

Is it possible multiple methods with the same name but different parameters in a class?

From Dev

Is it possible in Play 2.1 to convert a JsValue to a case class instance if you only know the name of the case class at the runtime?

From Dev

Possible loss of precision between two different compiler configurations

From Dev

Possible loss of precision between two different compiler configurations

From Dev

merge mysql rows by name when names are slightly different

From Dev

Different between two statements

From Dev

Ruby how to merge two CSV files with slightly different headers

From Dev

Custom validation for two fields with same validation but slightly different

From Dev

Scala: convert map to case class

From Dev

Is this the best way to convert between two different types of array?

From Dev

Is this the best way to convert between two different types of array?

From Dev

Converting between different implementations of abstract class with generic convert method

From Dev

PHP : Whats is a different between self:: and class_name::?

Related Related

  1. 1

    Is it possible to extract same field with two different name through regex?

  2. 2

    Scala Case Class with Different Argument Types

  3. 3

    Deserialize two slightly different JSON strings (same structure, different names) to the same class

  4. 4

    Differentiate between two strings with different case in mysql

  5. 5

    why does a scala class in the worksheet with same name but different case as the worksheet cause an exception?

  6. 6

    is it possible to calculate data between two different cubes?

  7. 7

    Is it possible to ssh between two different network?

  8. 8

    Same use case, different actors, slightly different scenarios

  9. 9

    Pascal - Class inheritance between two different files?

  10. 10

    Using shapeless scala to merge the fields of two different case classes

  11. 11

    possible unique combinations between between two arrays(of different size) in python?

  12. 12

    possible unique combinations between between two arrays(of different size) in python?

  13. 13

    scala/spark different case class based on input data

  14. 14

    Scala case class whose fields can be mandatory and optional at different instances

  15. 15

    Git remote two branches same name different case

  16. 16

    Extract scala case class field

  17. 17

    Is it possible multiple methods with the same name but different parameters in a class?

  18. 18

    Is it possible in Play 2.1 to convert a JsValue to a case class instance if you only know the name of the case class at the runtime?

  19. 19

    Possible loss of precision between two different compiler configurations

  20. 20

    Possible loss of precision between two different compiler configurations

  21. 21

    merge mysql rows by name when names are slightly different

  22. 22

    Different between two statements

  23. 23

    Ruby how to merge two CSV files with slightly different headers

  24. 24

    Custom validation for two fields with same validation but slightly different

  25. 25

    Scala: convert map to case class

  26. 26

    Is this the best way to convert between two different types of array?

  27. 27

    Is this the best way to convert between two different types of array?

  28. 28

    Converting between different implementations of abstract class with generic convert method

  29. 29

    PHP : Whats is a different between self:: and class_name::?

HotTag

Archive