最初にエラーが発生した後に検証が停止するように、以下の検証関数を記述しようとしています。返品タイプはthree
で、他の機能と異なります。このコードをコンパイルするために使用するモナド・トランスはどれですか?どのMonad Transformerを使用しますか?
import scalaz._
import Scalaz._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
def one(a : String): Disjunction[Int, String] =
a == "one" match {
case true => \/-("one")
case false => -\/(2)
}
def two(a : String): Disjunction[Int, String] =
a == "two" match {
case true => \/-("two")
case false => -\/(3)
}
def three(a : String): Future[Disjunction[Int, String]] =
Future (a == "three") map {
case true => \/-("three")
case false => -\/(4)
}
def validate(a : String) = for {
e1 <- one(a)
e2 <- two(a)
e3 <- EitherT(three(a))
} yield (e1 |+| e2 |+| e3)
コンパイルエラー:
Error:(27, 7) type mismatch;
found : scalaz.EitherT[scala.concurrent.Future,Int,String]
required: scalaz.\/[?,?]
e3 <- EitherT(three(a))
^
Error:(66, 7) type mismatch;
found : scalaz.EitherT[scala.concurrent.Future,Int,String]
required: scalaz.\/[?,?]
e3 <- EitherT(three(a))
^
'' future''や ''wait''の' three'の結果に 'one'と' two'をラップしない理由は何ですか? –
あなたは 'EitherT'モナドトランスフォーマーを見たいと思っています.3番目の' < - 'は' String'を返さないので、あなたが望むものではないかもしれない 'Disjunction'全体を与えています(最初の2つは'ストリング(string)」)。 –
@EndeNeu EitherTを試しましたが、まだコンパイルエラーが発生しています。コンパイルエラーとともに更新された質問をご覧ください。私は前にDisjunctionTを使用していました(これはEitherTの単なるラッパーです)。 –