Is there a built-in support for monad that deals with exception handling? Something similar to Scala's Try. I am asking because I don't like unchecked exceptions.
相关问题
- Understanding do notation for simple Reader monad:
- Which class does LongAdder extends?
- Single element in multiple groups when grouping wi
- Carry STRef implicitly in an environment during co
- JVM crashes with problematic frame [libjvm.so+0x7f
相关文章
- ceil conterpart for Math.floorDiv in Java?
- Do JDK classes have any further specifications bey
- java.time.ZonedDateTime.parse and iso8601?
- Java “static import” vs. “import static” in Java 8
- Will java allow to use functional interfaces as me
- How to enable “type information” for streams retur
- Unable to open keystore in AndroidStudio - “Redund
- Wrong implementation of Oracle Java ConcurrentHash
@Misha is onto something. Obviously you wouldn't do this exact thing in real code, but
CompletableFuture
provides Haskell-style monads like this:return
maps toCompletableFuture.completedFuture
>=
maps tothenCompose
So you could rewrite @Misha's example like this:
which maps to the Haskell-ish:
or with do syntax
Implementations of
fmap
andjoin
I got a little carried away. These are the standard
fmap
andjoin
implemented in terms ofCompletableFuture
:Firstly, let me apologise for answering instead of commenting - apparently I need 50 reputation to comment ...
@ncaralicea your implementation is similar to my own but the problem I had was how to reconcile the try ... catch in bind() with the identity laws. Specifically return x >>= f is equivalent to f x. When bind() catches the exception then f x differs because it throws.
Moreover the ITransformer appears to be a -> b instead of a -> M b. My current version of bind(), unsatisfactory though I find it, is
where value is an
The problem with the identity law is that it requires the function f to catch exceptions which defeats the whole purpose of the exercise.
What I think you might actually want is the Functor and not the Monad. That is the fmap : (a->b) -> f a -> f b function.
If you write
then you don't need to write explicit exception handling code, implement new interfaces or mess with the Monad laws.
Here there is an implementation that could be used as a model. Further information can be found here:
Java with Try, Failure, and Success based computations
You can basically do something like this:
And here is the code:
I hope this might shade some light.
The "better-java-monads" project on GitHub has a Try monad for Java 8 here.
You can do what you want by (ab)using
CompletableFuture
. Please don't do this in any sort of production code.There are at least two generally available (e.g. on Maven Central) - Vavr and Cyclops both have Try implementations that take a slightly differing approach.
Vavr's Try follows Scala's Try very closely. It will catch all 'non-fatal' exceptions thrown during the execution of it's combinators.
Cyclops Try will only catch explicitly configured exceptions (of course you can, by default, also have it catch everything), and the default mode of operating is only to catch during the initial population method. The reasoning behind this is so that Try behaves in a somewhat similar way to Optional - Optional doesn't encapsulate unexpected Null values (i.e. bugs), just places where we reasonable expect to have no value.
Here is an example Try With Resources from Cyclops
And another example 'lifting' an existing method (that might divide by zero) to support error handling.