I'm trying to get going in Scala from the Twitter Scala School but am stumbling over syntax errors. When I run the Pattern Matching code from the "Basics continued" tutorial http://twitter.github.io/scala_school/basics2.html#match through my sbt console the compiler turns me back with "error: not found: value &&". Has something changed in Scala to take what probably worked when the tutorial was written but doesn't work now? The classes involved are
class Calculator(pBrand: String, pModel: String) {
/**
* A constructor
*/
val brand: String = pBrand
val model: String = pModel
val color: String = if (brand.toUpperCase == "TI") {
"blue"
} else if (brand.toUpperCase == "HP") {
"black"
} else {
"white"
}
// An instance method
def add(m: Int, n: Int): Int = m + n
}
class ScientificCalculator(pBrand: String, pModel: String) extends Calculator(pBrand: String, pModel: String) {
def log(m: Double, base: Double) = math.log(m) / math.log(base)
}
class EvenMoreScientificCalculator(pBrand: String, pModel: String) extends ScientificCalculator(pBrand: String, pModel: String) {
def log(m: Int): Double = log(m, math.exp(1))
}
My repl looks something like this...
bobk-mbp:Scala_School bobk$ sbt console
[info] Set current project to default-b805b6 (in build file:/Users/bobk/work/_workspace/Scala_School/)
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.9.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_17).
Type in expressions to have them evaluated.
Type :help for more information.
...
scala> 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"
| }
<console>:9: error: not found: value &&
case calc.brand == "hp" && calc.model == "20B" => "financial"
^
<console>:10: error: not found: value &&
case calc.brand == "hp" && calc.model == "48G" => "scientific"
^
<console>:11: error: not found: value &&
case calc.brand == "hp" && calc.model == "30B" => "business"
^
scala>
How to I get the use case of AND on my cases when I'm doing Matching on Class Members?
Thanks in advance. I'm new to this.