Tuesday, August 4, 2009

Basic Matching

Matching is a powerful concept in scala that is similar to a switch in Java. The difference is that matching takes it to another level. We will start with basic switching in this entry.

Note. If a match is not found a scala.MatchError is thrown
  1. scala>val s = "sampleString"
  2. s: java.lang.String = sampleString
  3. scala> s match {
  4.      | case"sampleString" => "its a match!"
  5.      | case _ => "no match"
  6.      | }
  7. res0: java.lang.String = its a match!
  8. scala> s match {
  9.      | case m @"sampleString" => "its a match!"
  10.      | case _ => "no match"
  11.      | }
  12. res1: java.lang.String = its a match!
  13. scala> s match {
  14.      | case m @"sampleString" => "we found "+m+"!"
  15.      | case _ => "no match"
  16.      | }
  17. res2: java.lang.String = we found sampleString!
  18. scala>  s match {
  19.      | case m if(m.startsWith("s") ) => "its a match!"
  20.      | case _ => "no match"
  21.      | }
  22. res3: java.lang.String = its a match!
  23. scala>  s match {
  24.      | case m => "this matches everything"
  25.      | }
  26. res4: java.lang.String = this matches everything
  27. scala>val obj:Any = s // I am assigning s to a variable whose type is Any (parent of all object).
  28. obj: Any = sampleString
  29. scala>  obj match {
  30.      | case string:String => "we found a string"
  31.      | case integer:Int => "we found an integer"
  32.      | case _ => "hmmm don't know what it is"
  33.      | }
  34. res6: java.lang.String = we found a string
  35. scala>// Now  we will see what happens when a match fails
  36. scala>  obj match {
  37.      | case integer:Int => "we found an integer"
  38.      | case double:Double => "we found an double"
  39.      | }
  40. scala.MatchError: sampleString
  41.                   at .(:7)
  42.                   at .()
  43.                   at RequestResult$.(:3)
  44.                   at RequestResult$.()
  45.                   at RequestResult$result()
  46.                   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  47.                   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  48.                   at sun.reflect.DelegatingMethodAccessorImpl.i...
  49. scala>

4 comments:

  1. Hi,
    It is better to add some explanation rather than just giving code snippet. For example what is the difference between case "sampleString" and case m @ "sampleString".
    Thanks,
    Senthil

    ReplyDelete
  2. m is name for whole matching term which can have many parts.

    ReplyDelete
  3. Question is still opened.
    What m @ "sampleString" means?

    ReplyDelete
  4. The difference between

    case "sampleString" => ...

    and

    case m @ "sampleString" => ...


    is that "sampleString" is assigned to m. In this example it is not too useful but consider the code:

    functionThatReturnsList() match {
    case m @ "s1" :: "s2" :: "s3" :: _* => // use m
    case _ => // no match
    }

    so in this case we don't have to assign *functionThatReturnsList()* to a val since that assignment is inline within the case call.

    ReplyDelete