从 Unit 到 Future[Boolean] 的类型转换
Type conversion from Unit to Future[Boolean]
我有以下功能,我想returnFuture[Boolean]
但是IDE提示我returnUnit
。我是 Scala 的新手。有人可以指出我做错了什么吗?
def remove(loginInfo: LoginInfo): Future[Boolean] = {
val result = findObject(loginInfo)
result.onSuccess {
case Some(persistentPasswordInfo) =>
val removeResult = remove(persistentPasswordInfo._id.toString)
removeResult.map {
case Left(ex) => Future.successful(false)
case Right(b) => Future.successful(b)
}
case None => Future.successful(false)
}
}
将 onSuccess
替换为 flatMap
。假设您的 remove(x: String)
方法也 returns 一个 Future
,那也需要 flatMap
ped:
def remove(loginInfo: LoginInfo): Future[Boolean] = {
val result = findObject(loginInfo)
result.flatMap {
case Some(persistentPasswordInfo) =>
val removeResult = remove(persistentPasswordInfo._id.toString)
removeResult.flatMap {
case Left(ex) => Future.successful(false)
case Right(b) => Future.successful(b)
}
case None => Future.successful(false)
}
}
我有以下功能,我想returnFuture[Boolean]
但是IDE提示我returnUnit
。我是 Scala 的新手。有人可以指出我做错了什么吗?
def remove(loginInfo: LoginInfo): Future[Boolean] = {
val result = findObject(loginInfo)
result.onSuccess {
case Some(persistentPasswordInfo) =>
val removeResult = remove(persistentPasswordInfo._id.toString)
removeResult.map {
case Left(ex) => Future.successful(false)
case Right(b) => Future.successful(b)
}
case None => Future.successful(false)
}
}
将 onSuccess
替换为 flatMap
。假设您的 remove(x: String)
方法也 returns 一个 Future
,那也需要 flatMap
ped:
def remove(loginInfo: LoginInfo): Future[Boolean] = {
val result = findObject(loginInfo)
result.flatMap {
case Some(persistentPasswordInfo) =>
val removeResult = remove(persistentPasswordInfo._id.toString)
removeResult.flatMap {
case Left(ex) => Future.successful(false)
case Right(b) => Future.successful(b)
}
case None => Future.successful(false)
}
}