[英]Scala Anorm Why is scala complaining about result not having a trait?
我还没有看到完成此操作的任何地方,但有以下几点:
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]]
还是不够的?
给定错误消息,听起来Game.participantParser
不是返回Option[List[Player]]
而是List[Player]
。 恕我直言, getPlayers
应该只返回可能为空的List[Player]
。
如果要返回Option[]
,则必须在列表前面添加Some(...)
,如果列表为空,则只返回None
。
如评论中所述,如果没有Game.participantParser
定义,就无法进一步帮助您。
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.