For value: Any
I need to check one string case.
For rest cases should return value
itself.
What is the right syntax: case _ => _ ?
def foo(value: Any) = value match {
case x: String => if (x == "cond") None else x
case _ => _ // Compiler -> Not found value x$1. Unbound placeholder parameter
}
Simply use some (arbitrary) identifier rather than _
:
def foo(value: Any) = value match {
case x: String => if (x == "cond") None else x
case other => other
}
However, it is not best practice to return None
in some cases, if you're not returning Some
in the other cases. A more correct version would be:
def foo(value: Any) = value match {
case "cond" => None
case other => Some(other)
}
Then in either case you have an object of type Option
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With