4 ms·
The unfortunate stance on ADTs is that Java doesn't want them generalized. The standard answer is use Records and Sealed classes. What does this mean? Well wit
by karmakaze 5y ago
The unfortunate stance on ADTs is that Java doesn't want them generalized. The standard answer is use Records and Sealed classes.
What does this mean? Well with generics we can have two libraries that know about Map<> and concrete types TypeA and TypeB and can interface type-safely using Map<TypeA, TypeB>.
What we can't do is have two libraries that know about TypeA and TypeB interface type-safely using a sealed type of TypeA and TypeB. Even without considering libraries, another use we may want to talk about a sealed type of TypeA and TypeC, but of course TypeA can only be in one of the sealed class trees.
The worst part is that Java already has a Union type that's only ever used in multi catch(ExceptionA|ExceptionB e).
- karmakaze 5y agoI've been thinking about the lack of ad-hoc SumTypes in Java. The sealed class approximation isn't good enough. I'm seriously considering taking up TypeScript in earnest as my 'go to' language. It's an outlier in supporting A | B | C and being mainstream with pretty good runtime performance. One thing I'd have to check is the quality of IDE support for refactoring tooling. Refactoring operations for Java using IntelliJ almost always works and as I expect or better.