Reputation: 117
I'm new to scala and just learning to code . can someone tell me what am i doing wrong here
class ForExtractor(val i : Int , j:Int){
//def unapply(i:Int) = true
}
object ForExtractor{
def unapply(i:Int ):Option[(Int , Int)] = if(i>2)Some(1,2) else None
}
def extractorTest(obj : ForExtractor) = {
obj match {
case ForExtractor(4,2)=> true ;
case e => false
}
}
The error that i see on case line is
pattern type is incompatible with expected type ; found:Int ,
ForExtractor
Upvotes: 1
Views: 47
Reputation: 651
What i can guess is that you want to test whether the val i
in your ForExtractor is bigger that 2 or not, and in this case return Some((1,2)) (notice your error here you're returning Some(1,2)
).
your unapply method should take as argument a ForExtractor, so in the end the unapply method would look like this:
def unapply(forex: ForExtractor):Option[(Int , Int)] =
if(forex.i > 2)Some((1,2)) else None
then we get:
scala> extractorTest(new ForExtractor(1, 2))
res1: Boolean = false
Upvotes: 1