I tried to create an unapply
method to use in pattern matching, and I tried to make it return something different than Option
, however, Eclipse shows that as an error. Is it a rule that unapply
must return an Option[T]
?
EDIT: here's the code I'm trying to use. I switched the code from the previous section so that unapply
returns a Boolean
import java.util.regex._
object NumberMatcher {
def apply(x:String):Boolean = {
val pat = Pattern.compile("\\d+")
val matcher = pat.matcher(x)
return matcher.find
}
def unapply(x:String):Boolean = {
val pat = Pattern.compile("\\d+")
val matcher = pat.matcher(x)
return matcher.find
}
}
object x {
def main(args : Array[String]) : Unit = {
val strings = List("geo12","neo493","leo")
for(val str <- strings) {
str match {
case NumberMatcher(group) => println(group)
case _ => println ("no")
}
}
}
}
Eclipse says wrong number of arguments for object NumberMatcher
. Why is that?