3 ms·
> discriminated unions Java now has sealed classes/interfaces. They are sum types, not sure what is actually the difference between discriminated unions. And w
by kaba0 6y ago
> discriminated unions
Java now has sealed classes/interfaces. They are sum types, not sure what is actually the difference between discriminated unions. And with records, you have named tuples, basically product types. As far as I know, Haskell also only has algebraic datatypes.
The syntax is like so:
```
sealed interface Expression permits Add, Multiply {}
record Add(Expression a, Expression b) implements Expression {}
```
(written on phone so it may not actually compile)
And you also have switch expressions enforcing that you have used up every possibility (even null!), and with the coming better pattern matching, Java will become quite an expressive language.
- zumu 6y agoThanks for bringing this up. I am actually very excited about these features, but they are still not quite available for most of us Java devs yet. The LTS version that contains them doesn't release until September, and it will take months (if not years) for many shops to upgrade. My critique was more targeted to the Java of the time period this article was written in (2014), but these new additions are another point for the "Just write it in Java" folks.