2015-12-22 2 views
3

J'essaie d'utiliser Scalaz EitherT avec un scala.concurrent.Future. Lorsque vous essayez de l'utiliser dans un pour-compréhension:Instances Functor et Monad manquantes lors de l'utilisation de scala.concurrent.Future avec EitherT

import scalaz._ 
import Scalaz._ 

val et1:EitherT[Future, String, Int] = EitherT(Future.successful(1.right)) 

val et2:EitherT[Future, String, String] = EitherT(Future.successful("done".right)) 

val r:EitherT[Future, String, String] = for { 
    a <- et1 
    b <- et2 
} yield (s"$a $b") 

-je obtenir les instances Functor et Monad suivants manquant Erreur:

could not find implicit value for parameter F: scalaz.Functor[scala.concurrent.Future] 
b <- et2 
^
could not find implicit value for parameter F: scalaz.Monad[scala.concurrent.Future] 
a <- et1 

Est-ce que scalaz définir des instances pour Functor et Monad pour l'avenir? Sinon, y a-t-il d'autres bibliothèques qui fournissent ces instances ou dois-je les écrire?

Répondre

9

Vous avez besoin d'un ExecutionContext implicite dans la portée. import ExecutionContext.Implicits.global vous obtiendrez le global execution context.

Exemple complet:

import scala.concurrent.ExecutionContext.Implicits.global 

    import scalaz._ 
    import Scalaz._ 

    val et1:EitherT[Future, String, Int] = EitherT(Future.successful(1.right)) 

    val et2:EitherT[Future, String, String] = EitherT(Future.successful("done".right)) 

    val r:EitherT[Future, String, String] = for { 
    a <- et1 
    b <- et2 
    } yield s"$a $b" 

    val foo = Await.result(r.run, 1 seconds) 
    // => \/-("1 done") 
+0

qui ne semble pas fonctionner. – ssanj

+0

Cela fonctionne pour moi. J'ai mis à jour ma réponse avec l'exemple complet. – danielnixon

+0

oui, ça marche! J'ai dû manquer quelque chose quand j'ai essayé avant. Merci. – ssanj