Lifting a Future[T] into a Future[Either[Throwable, T]]

269 views
Skip to first unread message

Chris Marshall

unread,
Jun 1, 2012, 8:18:21 AM6/1/12
to scala...@googlegroups.com
The new concurrent Future class ( http://www.scala-lang.org/archives/downloads/distrib/files/nightly/docs/library/index.html#scala.concurrent.Future) should (I think) have a mechanism to lift a Future[T] into a Future[Either[Throwable, T]] (the method could be called "liftEither", or just "e").

The reason that this is useful is that it means we can go through the following process:

CC[Future[T]] ~> CC[Future[Either[Throwable, T]]] ~> CC[Future[ValidationNEL[Throwable, T]] ~> Future[ValidationNEL[Throwable, CC[T]]]

It also means that the future can be used in a for-comprehension without "silently" ignoring failure:

for (e <- intFuture.e) yield e.right.map( ... etc )

Is this worthy of an RFE? It's an incredibly simple library addition

Chris

Josh Suereth

unread,
Jun 1, 2012, 9:02:30 AM6/1/12
to Chris Marshall, scala...@googlegroups.com
Might be more useful to have a general "mapResult" that looks like:

def mapResult[B](f: Either[Throwable,T] => Either[Throwable,B]): Future[B]

That way you could lift the result directly into a Validation if you wanted...

√iktor Ҡlang

unread,
Jun 1, 2012, 9:16:12 AM6/1/12
to Chris Marshall, scala...@googlegroups.com
def lift[T](f: Future[T]): Future[Either[Throwable, T]] = f map Right.apply recover { case t => Left(t) } ?

Cheers,
 

Chris



--
Viktor Klang

Akka Tech Lead
Typesafe - The software stack for applications that scale

Twitter: @viktorklang

Kevin Wright

unread,
Jun 1, 2012, 9:22:40 AM6/1/12
to √iktor Ҡlang, Chris Marshall, scala...@googlegroups.com
That's what I do :)

It's the only way to get sane behaviour from Future.sequence/traverse

Chris Marshall

unread,
Jun 1, 2012, 9:49:06 AM6/1/12
to viktor...@gmail.com, scala...@googlegroups.com
Does that count as agreement that it's a good idea to add?


Date: Fri, 1 Jun 2012 15:16:12 +0200
Subject: Re: [scala-user] Lifting a Future[T] into a Future[Either[Throwable, T]]
From: viktor...@gmail.com
To: oxbow...@hotmail.com
CC: scala...@googlegroups.com

√iktor Ҡlang

unread,
Jun 1, 2012, 9:59:59 AM6/1/12
to Chris Marshall, scala...@googlegroups.com
On Fri, Jun 1, 2012 at 3:49 PM, Chris Marshall <oxbow...@hotmail.com> wrote:
Does that count as agreement that it's a good idea to add?

What should happen if you lift a Future[Either[Throwable, T]]?

Chris Marshall

unread,
Jun 1, 2012, 11:26:02 AM6/1/12
to viktor...@gmail.com, scala...@googlegroups.com
You should get a Future[Either[Throwable, Either[Throwable, T]]] and it will be your own fault if you do.


Date: Fri, 1 Jun 2012 15:59:59 +0200
Reply all
Reply to author
Forward
0 new messages