Updating pattern matching on class members #89

Merged
merged 1 commit into from Apr 25, 2013

Conversation

Projects
None yet
2 participants
Contributor

kevinrobinson commented Apr 25, 2013

In an awesome Scala school session with @jjmmcc, it came up that one of the pattern matching example isn't valid syntax. This is what I get in 2.9.2:

Welcome to Scala version 2.9.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_45).
Type in expressions to have them evaluated.
Type :help for more information.

scala> case class Calculator(brand: String, model: String)
defined class Calculator

scala> :paste
// Entering paste mode (ctrl-D to finish)

def calcType(calc: Calculator) = calc match {
  case calc.brand == "hp" && calc.model == "20B" => "financial"
  case calc.brand == "hp" && calc.model == "48G" => "scientific"
  case calc.brand == "hp" && calc.model == "30B" => "business"
  case _ => "unknown"
}

// Exiting paste mode, now interpreting.

<console>:10: error: not found: value &&
         case calc.brand == "hp" && calc.model == "20B" => "financial"
                                 ^
<console>:11: error: not found: value &&
         case calc.brand == "hp" && calc.model == "48G" => "scientific"
                                 ^
<console>:12: error: not found: value &&
         case calc.brand == "hp" && calc.model == "30B" => "business"
                                 ^
scala> calc match {
     | case calc.brand == "hp" => "hhhhhhp"
     | case _ => "unknown"
     | }
<console>:8: error: not found: value calc
              calc match {
              ^
<console>:9: error: value == is not a case class constructor, nor does it have an unapply/unapplySeq method
              case calc.brand == "hp" => "hhhhhhp"
                              ^

So I updated the example.

Contributor

lahosken commented Apr 25, 2013

Thanks for the fix. Syntax errors were not the kind of pain we were aiming for there :-P

lahosken added a commit that referenced this pull request Apr 25, 2013

Merge pull request #89 from kevinrobinson/patch/case-member-matching
Updating pattern matching on class members

@lahosken lahosken merged commit ff7f229 into twitter:master Apr 25, 2013

@kevinrobinson kevinrobinson deleted the kevinrobinson:patch/case-member-matching branch Apr 26, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment