【发布时间】:2013-06-10 23:27:19
【问题描述】:
我正在尝试从 Twitter Scala School 开始学习 Scala,但遇到语法错误。当我通过我的 sbt 控制台运行“基础知识继续”教程http://twitter.github.io/scala_school/basics2.html#match 中的模式匹配代码时,编译器将我返回为“错误:未找到:值 &&”。 Scala 中是否发生了一些变化,以采用在编写教程时可能起作用但现在不起作用的东西?所涉及的类是
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))
}
我的 repl 看起来像这样......
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>
当我对类成员进行匹配时,如何在我的案例中获取 AND 的用例?
提前致谢。我是新手。
【问题讨论】:
-
顺便说一句,您使用的是过时的 Scala 版本。当前版本是
2.10.2。 -
当我强制 SBT 控制台使用更新版本的 Scala 时,结果是一样的。
-
是的,当然,但是过时的版本就是过时的版本......
标签: scala