I have a scala function with the following signature:
def getValue: Validation[ Throwable, String ]
Now, I want to process in Java the result (say res) of calling this function.
I can call res.isSuccess() and res.isFailure() but how do I extract the underlying Throwable or success String ?
I tried casting but testing (res instanceof scalaz.Failure) results in a scalaz.Failure cannot be resolved to a type (the scala and scalaz jars are visible)
Is there a generic way of doing this for Validation, Option, Either, etc… ?
EDIT I realize I can probably use a fold but that would lead to a lot of ugly boilerplate code in Java (which does not need more). Anything better looking ?
SuccessandFailureare both case classes:So you can write the following, for example:
For
OptionandEitheryou can use thegetmethod (after taking theleftorrightprojection in the case ofEither).If at all possible it’s probably cleaner to do this kind of unwrapping with a
fold, etc. on the Scala side, though.