Addition to scala pattern matching

| | Comments (1) | TrackBacks (0)
I recently posted a blog post about scala pattern matching. It is intended to be an introduction to pattern matching concepts using Scala as the implementation. I have been writing more scala code recently and I have come to the conclusion that pattern matching is missing one thing to really make it developer friendly, useful error messages!

Pattern Matching: What is missing?

Useful error messages. Huh? Why? What are you proposing?


When all goes well

The code block associated with the case is executed. All is fine here.


When there is no match

A scala.MatchError exception is thrown. Herein lies the problem. As a developer, I get a MatchError exception with a string that says that there was a match error. If I look at the stack trace I can even figure out where things went wrong in the code. But why can't I provide the match statement a String with a more developer/user friendly error message? Why can't I say what there was no match for? For example, suppose I am matching days of the week. Wouldn't a message like "Unable to match the day of the week." be more useful then a general error simply stating that a MatchError was thrown?


When there is a type mismatch

If the data type of the thing being matched cannot be applied to any case in the match that gets executed a scala.MatchError is thrown. Since this is a different type of error I would contend that it is fine as is.


Proposed modification

What if the match was passed a string with a user/developer friendly error message? It might look something like this:

  val user = "harry"
  user match "Unable to match the name against the list of Weasley boys" {
    case "fred" =>
      println("this would not be good")
    case "george" =>
      println("this also would not be good")
    case "ron" =>
      println("again, not good")
    case "percy" =>
      println("still not good")
  }
So instead of seeing:

Caused by: scala.MatchError: harry

You could see:

Caused by: scala.MatchError: Unable to match the name against the list of Weasley boys

Hmmm... it seems that we are still missing the value that wasn't matched, so what about a Function?
  val user = "harry"
  user match (n) => "Unable to match the name " + n + " against the list of Weasley boys" {
    case "fred" =>
      println("this would not be good")
    case "george" =>
      println("this also would not be good")
    case "ron" =>
      println("again, not good")
    case "percy" =>
      println("still not good")
  }

Or the ultimate version:

Caused by: scala.MatchError: Unable to match the name harry against the list of Weasley boys


So to summarize, I would like to see match modified to be 1 of 3 possibilities:
  1. As is :)
  2. Match would take a string and use that as the message
  3. Match would take a callback function and pass it the value not matched
PS If there are more Weasley boys.. fine, that's beside the point!

1 Comment

Tommy Li said:

At the end of your match statemement try:

val user = "harry"
user match {
case "fred" =>
println("this would not be good")
case "george" =>
println("this also would not be good")
case "ron" =>
println("again, not good")
case "percy" =>
println("still not good")
case n =>
"Unable to match the name " + n + " against the list of Weasley boys"
}

Leave a comment

0 TrackBacks

Listed below are links to blogs that reference this entry: Addition to scala pattern matching.

TrackBack URL for this entry: http://www.nearinfinity.com/mt/mt-tb.cgi/378