I'm trying to define an ADT (tagged union type, actually).
enum MyEnum{
case MyFirstCase(value:String) extends MyEnum{
def someMethod = "whatever"
}
}
But the compiler complains... Is it possible to add methods specific cases of a Scala3 enum (GADT)?
Or do we have to rely with the usual sealed trait encoding?
It is possible with extension methods! See the example below.
The compiler will infer the enum type (here
MyEnum) instead of the precise case type (hereMyEnum.A.typein the example), so you need to specify it yourself in some cases.Note that Scala 3 also provide union types in the following form, which may be easier to use in your case: