Scala – Pattern matching always match first object

In a scala program, I had some code like

def remove(something: Any): Unit = {
  something match {
    case signature => ...
    case tls => ...
    case host => ...
    case port => ...
    case password => ...
    case _ => ...
  }
}

Where signature, tls, host, port, password were all objects defined like

object host

But when calling this method, passing an object, it always matched the first case “signature”.

The solution is to replace the objects in the cases with `object`

def remove(something: Any): Unit = {
  something match {
    case `signature` => ...
    case `tls` => ...
    case `host` => ...
    case `port` => ...
    case `password` => ...
    case _ => ...
  }
}

The solution comes from http://www.scala-lang.org/old/node/5223

Advertisements

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