【问题标题】:Scala Anorm Why is scala complaining about result not having a trait?Scala Anorm 为什么 scala 抱怨结果没有特征?
【发布时间】:2013-01-13 20:40:54
【问题描述】:

我还没有看到这样做的任何地方,但我有以下内容:

abstract class Player { val user_id: Long }
case class AlivePlayer(user_id: Long, target_id: Long) extends Player
case class DeadPlayer(user_id: Long) extends Player

def getPlayers(game: Game):Option[List[Player]] = DB.withConnection { implicit connect =>
  SQL(
    """
      SELECT * FROM game_participants WHERE game_id = {game_id})
    """
  ).on(
    'game_id -> game.id
  ).as(Game.participantParser *)
}

在这种情况下,我收到编译器错误并显示以下消息

error: type mismatch;  found   : anorm.ResultSetParser[List[Product with Serializable with models.Player]]  required: anorm.ResultSetParser[Option[List[models.Player]]]
).as(Game.participantParser *)

为什么我只指定返回类型为Option[List[Player]] 是不够的?

【问题讨论】:

  • 你能分享participantParser方法的定义吗?

标签: scala playframework-2.0 anorm playframework-2.1


【解决方案1】:

鉴于错误消息,听起来Game.participantParser 没有返回Option[List[Player]] 而是List[Player]。恕我直言,getPlayers 应该只返回一个可能为空的List[Player]

如果您想返回Option[],则必须在列表前面添加Some(...),或者如果列表为空,则只需返回None

正如 cmets 中所说,如果没有 Game.participantParser 定义,就无法进一步帮助您。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 2019-04-20
    • 2016-04-05
    • 1970-01-01
    相关资源
    最近更新 更多