3 ms·
This seems to be worse in every way to Optional. The throwing of a checked exception is ludicrous. It's a classic case of abusing checked exceptions for flow
by dfe 2y ago
This seems to be worse in every way to Optional.
The throwing of a checked exception is ludicrous. It's a classic case of abusing checked exceptions for flow control. The right approach is an isPresent() or isEmpty() call followed by a get() when you know it's present. Static analysis can ensure you don't screw this up.
Similarly, using then and thenMaybe instead of map and flatMap means it now has its own terminology different from Stream.
Being able to iterate it is dubious. You can already .stream() an Optional (to an empty or 1-element stream). You can also if (o.isPresent()) { o.get(); } which is hardly burdensome. I'm trying to figure out how a for-each loop is an improvement. But if you must: o.map(Collections::singleton).orElse(Collections.empty()). Or o.stream().toList().
I applaud the author for experimenting with different API designs, but this one is just not good. It causes more trouble than it solves.