【发布时间】:2016-11-07 06:04:12
【问题描述】:
我想在数据库中找到一些对象(战斗)并根据它的存在返回这个特定对象或在数据库中创建一个新对象并返回新创建的对象。我实现了以下功能:
def findOrCreateFight(firstBoxer: BoxersRow, secondBoxer: BoxersRow, eventDate: java.sql.Date): Future[FightsRow] = {
for {
fight <- findByBoxersAndDate(firstBoxer, secondBoxer, eventDate)
} yield {
fight match {
case Some(f) => f
case None => createAndFindFight(firstBoxer, secondBoxer, eventDate)
}
}
}
findByBoxersAndDate 函数返回 Future[Option[FightsRow]] 对象,createAndFindFight 函数返回 Future[FightsRow]。现在编译器在 createAndFindFight 函数的一行中显示错误:
类型不匹配;成立 : scala.concurrent.Future[models.Tables.FightsRow] 需要: models.Tables.FightsRow
好的,所以我需要在“无”的情况下得到这个 Future 的完整结果。我考虑过 onComplete 函数,但它返回 Unit,而不是所需的 FightsRow 对象。任何建议如何修复我的功能以获得最佳的可扩展效果? :)
最好的问候
【问题讨论】: