Twitter 스칼라 스쿨에서 스칼라로 가려고하지만 구문 오류가 발생했습니다. 패턴 매칭 코드를 실행하면 컴파일러가 "오류 : 찾을 수 없음 : 값 & &"이라는 메시지와 함께 다시 돌아옵니다. 스칼라에서 튜토리얼이 작성되었지만 현재 작동하지 않을 때 효과가 있었던 것을 가져 오도록 뭔가가 변경 되었습니까? 관련된 클래스는 클래스 구성원과 일치 "오류 : 찾을 수 없음 : 값 &&"
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>
어떻게 내가 집단 구성원에 일치하고있을 때의 내 경우에 사용 사례를 얻을 수 ...
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이 같이 보입니다입니까?
미리 감사드립니다. 나는 이것에 처음이에요. _
당신이, 당신이 calc
이있는 콘크리트 가치를 귀찮게하지 않는 것을 나타냅니다 만의로
calc match {
case _ if calc.brand == "hp" && calc.model == "20B" => "financial"
...
}
: 당신이 패턴 조건을 테스트 할 때
Btw, 당신은 스칼라의 오래된 버전을 사용합니다. 현재 버전은'2.10.2'입니다. – sschaef
SBT 콘솔에 더 최신 버전의 스칼라를 사용하게하면 결과가 동일합니다. –
물론,하지만 오래된 버전은 오래된 버전입니다 ... – sschaef