0

I am getting an error in the extractor step (unapply method call). The error message is: Wrong number of arguments for the extractors. found 2; expected 0

Can someone please help what is causing the error (where my misunderstanding is).

class ABC(val name:String, val age:Int)  //class is defined.

object ABC{
    def apply(age:Int, name:String) = new ABC(name, age)  
    def unapply(x:ABC) = (x.name, x.age)                   
}


val ins = ABC(25, "Joe")  //here apply method is in action.
val ABC(x,y) = ins        //unapply is indirectly called. As per my understanding , 25 and Joe suppose to be captured in x and y respectively. But this steps gives error.

1 Answer 1

-1

The error I get is

an unapply result must have a member def isEmpty: Boolean

The easiest way to fix this is to make unapply return an Option:

def unapply(x: ABC) = Option((x.name, x.age))
1
  • Thanks for the reply Tim. Can you please let me know why my understanding is incorrect? What is understand is: 'ins' will be decomposed using unapply method and the returned values would be captured in x and y respectively. 1 hour ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.