I see that there is abstraction available to convert Future<T> to Try<T> by using a toTry on
Future<T>
But the problem with this function is that if there is any exception thrown in in the Future or the future fails with an exception I am not able to preserve that exception when I convert it to Try
Ideally if I would be writing Scala, i would write an implicit def which would convert it to the way i want by preserving the Exception like implicit def [T]toTry (future: Future[T]): Try[T].
I can anytime return a Future<T> and then check whether it is a success or a failure and take dependent actions, but then it beats the usage of FP where I don't want to write any boilerplate code to achieve this.
Is there a way to achieve this in javaslang or is this more of an improvment ?
@danieldietrich
Yes, you are right. This makes sense. Unfortunately we would have to change the Value.toTry() contract. Currently we have
... a new {@code Failure(NoSuchElementException)} is returned ...
Just changing the behavior of Future.toTry() would violate the Liskov substitution principle. Breaking the behavioral subtyping could break applications.
I will target this change to 3.0.0. We will change the base type Value in the way that the conversion method toTry() may return a Failure of _any exception_ in the empty case.
@Vivek-Patil @danieldietrich Makes sense.
A few thoughts: I would recommend not using toTry here, instead preferring getValue().get() as that actually captures the semantics of the situation.
Regardless, what doesn't violate Liskov here would be to have the NoSuchElementException in Future actually encapsulate the exception itself, e.g. new NoSuchElementException("get on failed future", innerException); Theoretically then one could use recover or other semantics here or at the very least have a more informative log message than NoSuchElementException that squashes the actual failure.
Most helpful comment
Yes, you are right. This makes sense. Unfortunately we would have to change the Value.toTry() contract. Currently we have
Just changing the behavior of Future.toTry() would violate the Liskov substitution principle. Breaking the behavioral subtyping could break applications.
I will target this change to 3.0.0. We will change the base type Value in the way that the conversion method toTry() may return a Failure of _any exception_ in the empty case.