【发布时间】:2017-10-22 17:59:50
【问题描述】:
我正在使用带有 OptionT 的 scalaZ 的 monad 转换器 EitherT 做一个示例,但我有一个我不明白的编译错误。
这是我的示例代码
class EitherTMonadTransformer {
case class Error(msg: String)
case class User(username: String, email: String)
def authenticate(token: String): Future[Error \/ String] = Future {
\/.right("token")
}
def getUser(username: String): Future[Option[User]] = Future {
Some(User("paul", "osmosis_paul@gmail.com"))
}
val userObj: Future[\/[Error, Nothing]] =
(for {
username <- EitherT(authenticate("secret1234"))
user <- OptionT(getUser(username))
} yield user.username).run
@Test
def eitherTAndOptionT(): Unit = {
println(userObj)
}
}
编译错误说
Error:(32, 12) type mismatch;
found : scalaz.OptionT[scala.concurrent.Future,String]
required: scalaz.EitherT[scala.concurrent.Future,EitherTMonadTransformer.this.Error,?]
user <- OptionT(getUser(username))
知道有什么问题吗?
问候。
【问题讨论】: