A better (than Optional) maybe for Java
10 comments
·February 6, 2025dehrmann
kelseyfrog
I'd imagine it's a poor-man's do notation.
When it comes to structured programming, languages without a do syntax are considered incomplete. It's like programming without ifs.
porridgeraisin
Yeah I don't get the iterator thing either... Saw the implementation and I'm not sure what the usecase would be.
They have essentially implement a single element iterator for Some and a zero element iterator for None.
ai_
It's useful for combinators, like flatMap. For instance if you want to flatten an iterator of optional values into the values of all some elements.
blastonico
> The method names are more intuitive: e.g., then instead of map
But map makes total sense, considering that maybe is a monad.
edflsafoiewq
Rather than monads, map is more likely to be familiar from sequences, and an optional is just a sequence with length < 2.
rubenvanwyk
Or... Just use Kotlin?
> The exception that is raised when an empty Maybe is used is a checked exception so you don't forget to handle it.
I think they acknowledged get() should have been getOrThrow() so it's clear what can happen, but I don't know if making it checked is an improvement.
> It implements the Collection interface, so it can be accessed with a for-loop.
I'm curious what the use cases are for this. I've used Optional, it can be annoying, it's mostly around it being verbose and APIs being inconsistent. It was never wishing I could treat it as a collection.