I have:
val foo = Some(List(1, 2, 3)) -> Some("y")
I would like to cast match it:
foo match {
case (Some(x), Some(y)) => println(x + " " + y)
case _ => println("error")
This works fine for Some(List(1, 2, 3) -> Some("score"))
but fails for Some(List(1, 2, 3) -> None
, None -> Some("y")
or None -> None
with:
error: constructor cannot be instantiated to expected type;
found : Some[A]
required: None.type
error: not found: value ...
Why is that?
Of course I could use getOrElse()
but that does not look so elegant.
Thx a lot, Karsten
Update:
foo match {
case (x: Some[List[Int]], y: Some[Int]) => println(x.get)
case _ => println("error")
}
Fails as well with:
error: pattern type is incompatible with expected type;
found : Some[Int]
required: None.type
I would think that case _
would take care of that.