scala-hamsters / hamsters

A mini Scala utility library
Apache License 2.0
291 stars 23 forks source link

Add a monad transformer for FutureTry #114

Closed denisftw closed 6 years ago

denisftw commented 6 years ago

Hi!

I needed a FutureTry transformer and implemented it using your FutureEither and FutureOption as examples. If you want, I can submit a pull request with my additions. I think many people would benefit from having FutureTry as part of the library (I certainly would).

loicdescotte commented 6 years ago

Hi, Future[Try[_]] type is not really useful in practice because Future[T] can carry both the a T value and a Throwable, just like Try. So instead of building a Future[Try[T]] you can use Future.fromTry on your Try[T] to get a Future[T] :

val f1: Future[Int] = Future.fromTry(Try(1))
val f2: Future[Int] = Future.fromTry(Try(2))

val f3: Future[Int] = for {
  v1 <- f1
  v2 <- f2
} yield v1 + v2 

Does it fit your use case or would you still need a Future[Try] monad transformer?

denisftw commented 6 years ago

Hey @loicdescotte !

I agree that the standard Future can always be selected over Try as it can do everything that Try can do and more, but I would still argue that these types have slightly different meanings, and I personally tend to view failed Futures only as unrecoverable errors. Our current codebase heavily relies on Future[Throwable \/ Something] and I would very much like to switch from ScalaZ to Hamsters replacing disjunctions with Trys along the way.

I'm curious, however, what do you use with Play? I'm pretty sure that any sophisticated application will end up dealing with blocking code as well as something breakable. Would you use Future for both?

loicdescotte commented 6 years ago

@denisftw With Play I'm using Future, and Either type rather than Try. I understand what you're saying about the need of a Future[Try] usage, if it's useful for you it may certainly be useful for other people, you can send a PR for it :)

Thanks Loïc

denisftw commented 6 years ago

Yeah, I guess using Future[Either] with some custom error types is the most proper way™, but it also requires a bit more work, so I would settle on Future[Try] for now. Sending a PR then )