I am currently working on a project with Scala and it seems I don't fully understand Scala's Typesystem :-/
I have the following situation:
def reviews(id: Int) = Action { implicit request =>
Ok(html.products.reviews(
reviewlist,
reviewlist
.find(review => review.id == id)
.getOrElse(reviewlist.headOption)
))
}
Unfortunately the compiler says, he cannot convert Product to Option[Review], so I changed the code
reviewlist
.find(review => review.id == id)
.getOrElse(reviewlist.headOption)
with
id match {
case 0 => reviewlist.headOption
case id => reviewlist.find(review => review.id == id)
}
which seems to work now, even though its not exactly the same thing as it does, for example, not show the first record anymore if an invalid review id is being submitted. it will then pretend, that there are no reviews available yet.
I then broke the problem down to a veeery simple sample:
val a: Option[Int] = Some(1).getOrElse(Some(1))
So, has anyone an idea, why the expression on the right side is not of the type Option[Int]?? Both, Some(1) and None inherit directly from Option and this expression is actually Some(1) in any or am I wrong?
Interestinly enough
val a: Option[Int] = None.getOrElse(None)
works, but all other combinations do not...