Scala Pattern Matching: the reason to fall in love with Scala

Scala has a lot of interesting features and, among them, one of the most interesting ones is Pattern Matching. If you still need a reason to try Scala this is the one. Actually, I might be exaggerating a little bit, but this feature is really interesting. Really. 😉

In summary, Pattern Matching looks like switch statements in Java. But there are a couple of things to keep in mind:

  • Using switch statements in Java is usually a code smell, i.e., something to avoid, risking your good OO design. This is not the case in Scala, where pattern matching is a core feature of the language, and should be leveraged by your applications.
  • Unlike in Java, there is basically no limits in terms of what you can use in the case clause. This, combined with the fact that Scala is a functional language, letting you pass functions around, gives you insane flexibility.

Lets go through a (very) simple example so that you can see by yourself at least part of this power. Suppose you are writing a function that takes an object of any type, and prints some information about it. Here is how you could do this:

class PrettyPrinter {
  def print(obj: Any) = {
    obj match {
      case 1 => println("number one")
      case Client("john") => println("special client: john")
      case Client(name) => println("client: " + name)
      case other => println(other)
    }
  }
}
case class Client(name: String)

object PatternMatchingTest {
  def main(args: Array[String]): Unit = {
    var printer = new PrettyPrinter
    printer.print(1)
    printer.print(new Client("jcranky"))
    printer.print(new Client("john"))
    printer.print("RandomStuff")
  }
}

Notice that, in the same clause, we are matching:

  • an integer number;
  • a specific Client
  • any other Client
  • any object of any class

Very flexible. For instance, in the case where we match any client, notice also that we capture the client’s name “automagically”, and use it in the println call.

That’s all for now. What do you think? Is this powerful enough for you? =D

EDIT:

I forgot to mention the expected output of the code above… so here it is:


number one
client: jcranky
special client: john
RandomStuff

Advertisements
This entry was posted in scala and tagged , , , , , . Bookmark the permalink.

2 Responses to Scala Pattern Matching: the reason to fall in love with Scala

  1. Pingback: Matching a combination of class and trait in Scala « JCranky's Blog!

  2. Pingback: Fazendo uma combinação de class e trait no Scala | iMasters

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