soabase / soabase-halva

Idiomatic Scala ... in Java
https://github.com/soabase/soabase-halva/blob/master/README.md
Apache License 2.0
81 stars 5 forks source link

Add support for typed exception handling in suppliers to improve forComp useability #15

Open Alex-At-Home opened 8 years ago

Alex-At-Home commented 8 years ago

One really annoying thing in J8 is that there's no way of passing up typed exceptions, so if you have a long pipeline of lambdas calling functions that can throw them, you end up having to wrap the nice 1-liners in horrible try/catch routines.

I wrote a bunch of simple utilities to convert lambda to untyped exceptions here (eg): https://github.com/Alex-At-Home/Aleph2/blob/master/aleph2_data_model/src/com/ikanow/aleph2/data_model/utils/Lambdas.java#L64

So eg you'd do forComp(a, Utils.wrap(() -> FileUtils.openFile(...)))

One thing you could do in Halva specifically to help with for comprehensions is provide a similar util throwing an (untyped) LambdaException, and then catch those in the forComp code and have a Optional<LambdaException> For.pipelineError / or make it throw err.getCause() / whatever a sensible response to the error would be? (Could specify the policy with like forComp(..).ignoreErrors().etc or forComp(..).throwOnError().etc)

(In the general "monadic for" case you could also specify what to do with an error, eg make Optional become empty, switch a Validation to fail)