Ray H
Ray H

Reputation: 333

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]]?

Upvotes: 2

Views: 493

Answers (1)

teemoo
teemoo

Reputation: 881

Given the error message, it sounds that Game.participantParser is not returning an Option[List[Player]] but List[Player]. IMHO, getPlayers should just return a List[Player] that may be empty.

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.

And as said in the comments, without the Game.participantParser definition it is not possible to help you furthermore.

Hope this helps.

Upvotes: 1

Related Questions