logoalt Hacker News

lmmtoday at 8:38 AM3 repliesview on HN

> They have added proper algebraic data types to the language

No they haven't. E.g. they added a class that superficially looks like Option but subtly breaks the rules that Option is meant to follow, ensuring that no-one can ever manage to migrate existing codebases away from using `null`.


Replies

gf000today at 8:57 AM

Sealed classes/interfaces and records are proper sum and product types.

The stdlib's Option type predates this language update by a long shot, so it doesn't use sealed classes, but it is now possible to have the usual FP "Maybe" type in Java:

``` sealed class Maybe<T> permits Some, None { record Some<T>(T obj) {} record None() {} } ```

(You will probably have to write Maybe.Some and I might have messed up the generic syntax as I wrote it on my phone, but that's mostly how it looks)

show 2 replies
_kidliketoday at 8:52 AM

optional is not how algebraic data types are implemented in Java. Basically it's the combination of sealed types and records.

show 1 reply