[英]Scala Future for Comprehension
I have a method that is supposed to go over a Future and return a Tuple. 我有一种方法,应该遍历Future并返回一个元组。 Here it is:
这里是:
private def myMethod(id: Long): Future[(Double, Double)] = {
val result = for {
someValue <- run { getFromDb(id).headOption }
value <- someValue
} yield {
value.valueType match {
case ValueType.TypeA => (1.0, 1.0)
case ValueType.TypeB => (2.0, 2.0)
}
}
}
Here is my run method: 这是我的运行方法:
private def run[R](dbio: slick.dbio.DBIOAction[R, slick.dbio.NoStream, scala.Nothing]) = async {
await(db.run(dbio))
}
It uses the Scala Async library to time my calls to the database! 它使用Scala异步库来计时对数据库的调用!
the getFromDB method is just doing a query: getFromDB方法只是在做一个查询:
def getFromDb(myId: Long) {
MyTableQuery.filter(_.id === myId)
}
It complaints that myMethod returns a Future[Nothing]. 它抱怨myMethod返回一个Future [Nothing]。 I do not see any problems here.
我在这里看不到任何问题。 Any clues as to what might not satisfy the return type that I'm looking for?
关于什么可能不满足我要寻找的返回类型的任何线索?
Here's a simplified version which exhibits the same fault: 这是表现出相同故障的简化版本:
def method: Future[(Double, Double)] = {
val result = for {
someValue <- Future("a")
value <- Future("b")
} yield {
value match {
case "a" => (1.0, 1.0)
case "b" => (2.0, 2.0)
}
}
}
The problem is that you are capturing the return type in the result
value, so the method has no return type. 问题在于您正在捕获
result
值中的返回类型,因此该方法没有返回类型。 A valid form of this simplified function would be either of the following: 此简化功能的有效形式为以下任何一种:
def method: Future[(Double, Double)] = {
val result = for {
someValue <- Future("a")
value <- Future("b")
} yield {
value match {
case "a" => (1.0, 1.0)
case "b" => (2.0, 2.0)
}
}
result
}
Or: 要么:
def method: Future[(Double, Double)] = {
for {
someValue <- Future("a")
value <- Future("b")
} yield {
value match {
case "a" => (1.0, 1.0)
case "b" => (2.0, 2.0)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.