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

2 thoughts on “Don’t have any arguments in some cases?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s