Hacker News new | past | comments | ask | show | jobs | submit login

java:

    Optional<String> result = returnsAnOptional();
    if (result.isPresent()) {
        log.info("result is {}", result.get());
    }

    // and

    returnsAnOptional().map(r -> log.info("result is {}", r);



Optionals are a bit different though, since they don't provide a way to explicitly handle errors, just wrap the `null` value if it exists and force you to be more explicit about it.

I don't know that there's a clean way to do the Either monad in Java without algebraic data types. You could certainly make a wrapper class that maybe has an optional `Left` and `Right` private variable and then make a `map` that handles the unboxing of those, but I think that might be a bit messy.


my post was mostly facetious, but it is as you say: possible but probably not something anyone would want to use


Actually, I almost want to give it a try now. There's some messy try-catch Java code at work that might benefit.

Don't worry, I won't deploy it until my team has seen it ;)




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: