玩 Scala - 找到 Future[String] 但预期出现 String 错误
Play Scala - Found Future[String] but expected String error
我是 Play Scala 的新手。下面的代码片段我试图用来公开一个 API。它因以下错误而失败。
type mismatch;
found : scala.concurrent.Future[String]
required: String
API 来源:
def getStrategy(date: String) = Action.async {
val currentDate:String = toString(DateTime.now.minusDays(1))
getDecision(date, currentDate).map(lastError => Ok("No Strategy found:%s".format(lastError)))
}
def getDecision(reqestedDate:String, currentDate:String): Future[String] = {
getForecastPrice(reqestedDate).map(forecastPrice =>
getCurrentPrice(currentDate).map(currentPrice =>
getCall(currentPrice, forecastPrice)
)
)
}
def getForecastPrice(requestedDate:String): Future[Option[Double]] = {
predictionRepo.getPrediction(requestedDate).map( maybePrediction =>
maybePrediction.map ( fPrice => fPrice.price )
)
}
def getCurrentPrice(currentDate:String): Future[Option[Double]] = {
priceRepo.getPrice(currentDate).map ( maybePrice =>
maybePrice.map ( cPrice => cPrice.price )
)
}
def getCall(currentPrice:Option[Double], forcastPrice:Option[Double]): String = {
var decision = ""
println("currentPrice:" + currentPrice)
println("forcastPrice:" + forcastPrice)
if(currentPrice.isDefined && forcastPrice.isDefined) {
var currentPriceValue = currentPrice.get.toDouble
var forcastPriceValue = forcastPrice.get.toDouble
if((currentPriceValue*5/100) < (currentPriceValue - forcastPriceValue)) {
decision = "BUY"
} else if((currentPriceValue*5/100) > (currentPriceValue - forcastPriceValue)) {
decision = "SELL"
} else {
decision = "HOLD"
}
}
return decision
}
以上代码中的错误显示在以下位置。
getCurrentPrice(currentDate).map(currentPrice =>
能帮我找出这个问题的原因吗?
你能把getDecision
中的第一个map
改成flatMap
吗:
def getDecision(reqestedDate:String, currentDate:String): Future[String] = {
getForecastPrice(reqestedDate).flatMap(forecastPrice =>
getCurrentPrice(currentDate).map(currentPrice =>
getCall(currentPrice, forecastPrice)
)
)
}
使用当前代码,结果类型为 Future[Future[String]]
您可以使用 for comprehension 而不是在另一个地图中使用地图。示例代码应该是这样的。
for(
getForecastPriceResult <- getForecastPrice(requestedDate);
getCurrentPriceResult <- getCurrentPrice(currentDate)
) yield(getCall(getForecastPriceResult,getCurrentPriceResult))
我是 Play Scala 的新手。下面的代码片段我试图用来公开一个 API。它因以下错误而失败。
type mismatch;
found : scala.concurrent.Future[String]
required: String
API 来源:
def getStrategy(date: String) = Action.async {
val currentDate:String = toString(DateTime.now.minusDays(1))
getDecision(date, currentDate).map(lastError => Ok("No Strategy found:%s".format(lastError)))
}
def getDecision(reqestedDate:String, currentDate:String): Future[String] = {
getForecastPrice(reqestedDate).map(forecastPrice =>
getCurrentPrice(currentDate).map(currentPrice =>
getCall(currentPrice, forecastPrice)
)
)
}
def getForecastPrice(requestedDate:String): Future[Option[Double]] = {
predictionRepo.getPrediction(requestedDate).map( maybePrediction =>
maybePrediction.map ( fPrice => fPrice.price )
)
}
def getCurrentPrice(currentDate:String): Future[Option[Double]] = {
priceRepo.getPrice(currentDate).map ( maybePrice =>
maybePrice.map ( cPrice => cPrice.price )
)
}
def getCall(currentPrice:Option[Double], forcastPrice:Option[Double]): String = {
var decision = ""
println("currentPrice:" + currentPrice)
println("forcastPrice:" + forcastPrice)
if(currentPrice.isDefined && forcastPrice.isDefined) {
var currentPriceValue = currentPrice.get.toDouble
var forcastPriceValue = forcastPrice.get.toDouble
if((currentPriceValue*5/100) < (currentPriceValue - forcastPriceValue)) {
decision = "BUY"
} else if((currentPriceValue*5/100) > (currentPriceValue - forcastPriceValue)) {
decision = "SELL"
} else {
decision = "HOLD"
}
}
return decision
}
以上代码中的错误显示在以下位置。
getCurrentPrice(currentDate).map(currentPrice =>
能帮我找出这个问题的原因吗?
你能把getDecision
中的第一个map
改成flatMap
吗:
def getDecision(reqestedDate:String, currentDate:String): Future[String] = {
getForecastPrice(reqestedDate).flatMap(forecastPrice =>
getCurrentPrice(currentDate).map(currentPrice =>
getCall(currentPrice, forecastPrice)
)
)
}
使用当前代码,结果类型为 Future[Future[String]]
您可以使用 for comprehension 而不是在另一个地图中使用地图。示例代码应该是这样的。
for(
getForecastPriceResult <- getForecastPrice(requestedDate);
getCurrentPriceResult <- getCurrentPrice(currentDate)
) yield(getCall(getForecastPriceResult,getCurrentPriceResult))