Don’t have any arguments in some cases?

Here’s the problem I recently encountered:

abstract class CaseClass

case class NoArgs extends CaseClass{

def tryIt(arg: CaseClass) = arg match {
    case NoArgs => true
    ...
}

What would you expect? I expected it to work. But I got a compiler error:

error: pattern type is incompatible with expected type;
found   : object NoArgs
required: CaseClass

Well, solution is easy. Just add the parenthesis to the constructor. Looks shitty but that’s how it – obviously – should be, given it’s not an object.

abstract class CaseClass

case class NoArgs extends CaseClass{

def tryIt(arg: CaseClass) = arg match {
    case NoArgs() => true
    ...
}
Advertisements