简体   繁体   English

Scala Anorm为什么Scala抱怨结果没有特质?

[英]Scala Anorm Why is scala complaining about result not having a trait?

I haven't seen anywhere this is done but I have the following: 我还没有看到完成此操作的任何地方,但有以下几点:

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 *)
}

In this case, I get an compiler error with the following message 在这种情况下,出现以下消息时出现编译器错误

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

Why is it not sufficient for me to specify the return type solely as Option[List[Player]] ? 为什么仅将返回类型指定为Option[List[Player]]还是不够的?

Given the error message, it sounds that Game.participantParser is not returning an Option[List[Player]] but List[Player] . 给定错误消息,听起来Game.participantParser不是返回Option[List[Player]]而是List[Player] IMHO, getPlayers should just return a List[Player] that may be empty. 恕我直言, getPlayers应该只返回可能为空的List[Player]

If you want to return an Option[] , your will have to add Some(...) in front of your list or just return None if the list is empty. 如果要返回Option[] ,则必须在列表前面添加Some(...) ,如果列表为空,则只返回None

And as said in the comments, without the Game.participantParser definition it is not possible to help you furthermore. 如评论中所述,如果没有Game.participantParser定义,就无法进一步帮助您。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM