0

Is there an idiomatic way of generating something like a fromString method on a sealed trait for use with case classes?

For example, something like:

sealed trait ExampleEnum {def id: Any}

final case class One(first: String) extends ExampleEnum{ override def id = first}
final case class Two(first: String, second: String) extends ExampleEnum { override def id = (first, second) }

Where should def fromString(s: String): ExampleEnum exist? A companion object for the sealed trait? Something else? It feels like this "de-serialization" pattern would be something that exists or has a standard approach that would be found in existing packages.

cohoz
  • 750
  • 4
  • 16

1 Answers1

1

Use a companion object for the sealed trait.

cohoz
  • 750
  • 4
  • 16