Hacker News new | past | comments | ask | show | jobs | submit login

There is no real reason to have basic enums if you have ADTs , since those can easily provide everything a plain enum gives you.

I guess the name was just a historical artifact.




I agree (that’s what I meant under the more expressive part), though it is a bit more complex with identity in the way. Java’s enums are also the singleton pattern.

Java recently got ADTs as well in the form of sealed classes and records, here an enum would look like this:

  sealed interface Color permits Red, Blue, Green {
}

  record Red() {}
  record Blue() {}
  record Green() {}

You can now create as many Red instance as you want, they will be “equals” only under equals(), not under ==, while the latter is also true for the enum case.


The interface implementation types may include enums, though not sure that would help much in your example.


no the feature you're looking for is sealed classes, see e.g. Kotlin. Rust enum variants cannot be externally defined classes


That's completely orthogonal to ADT vs enum.


Enums enable two things: check current type from union of types and exhaustivity check in switch

The former is solved by reflection, the latter by sealed classes.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: