0

I have seen some examples to use ADT to extend data types to fit into either of the options but I am little confused on how to implement it in this kind of use case. In case classes I would create them and just add extends TraitName but what about primitive data types?

My sealed trait is AEMExpectedPayload which could be Booolean, Int, String, Seq[String],Seq[Boolean], or Seq[Int].

I want to do something like this:

sealed trait StringInt
String extends StringInt
Int extends StringInt

I see one way as:

sealed trait StringInt

case class Stringy(s : String) extends StringInt
case class Inty(s : Int) extends StringInt

However I wanted to confirm if this is the only way and nothing else?

Here's how I would want to use it:

val stringResult:StringInt = "test-string"
val intResult:StringInt = 22
Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
Shivam Sahil
  • 4,055
  • 3
  • 31
  • 62
  • 3
    https://stackoverflow.com/questions/3508077/how-to-define-type-disjunction-union-types – Dmytro Mitin Nov 22 '22 at 02:48
  • Nope that doesn't. The solution that's mentioned there is something I have already added in my question. I want to know if there's a better way then creating seperate case classes for each property or not? Because if that's the only way then I have to create alias objects too which would be a lot for all those properties – Shivam Sahil Nov 22 '22 at 02:51
  • I want to know if there's a way of doing it directly like: `Int extends CustomTrait` and not `case class Int(val:Int) extends CustomTrait` – Shivam Sahil Nov 22 '22 at 02:52
  • 3
    Please read the question linked attentively. There are several solutions there. Sealed trait hierarchy, union types (first-class in Scala 3 or different emulations in Scala 2), type classes, shapeless.Coproduct. – Dmytro Mitin Nov 22 '22 at 02:54
  • 3
    In Scala 3 you can use sum types: `type StringInt = String | Int`. In Scala 2 you cannot just lump 2 types without wrapping them in some tagged union. And that tagged union requires: explicitly creating sealed hierarchy, explicitly extending that sealed hierarchy, wrapping values into tagged union type. At best you could use implicit conversion to lift values for you, but that's it. Other option is using just `Either` - for 2 value sum type without special meaning they work great. – Mateusz Kubuszok Nov 22 '22 at 08:58
  • 2
    Theoretically, there is also an option to use constructs like https://milessabin.com/blog/2011/06/09/scala-union-types-curry-howard/ BUT it's non-standard, hard to reason for non-seasoned-library-developers and requires passing around implicit evidence, so it's developer-hostile and I never saw anyone trying to use it on a production. Long story short: stick to `Either`s or sealed + case classes with no extra magic. – Mateusz Kubuszok Nov 22 '22 at 09:00
  • Maybe something is not as it should be with your data model? Why do wrappers on `String` and `Int` only? Also, why not convert string to int or other way around and only keep a hint about type, but only if it is useful, like `Stringy(value: String, asType: IntType)` – user2963757 Nov 22 '22 at 12:06

0 Answers0