Scala match変数

2491 ワード

昨日Scalaを書いた時、match(switch相当)を使った時、運転結果がおかしいです.
 1 var i: Int = 0

 2 while (i < items.length) {

 3   i % width match {

 4     case offset => println("offset: " + items(i))

 5     case logSize => println("logSize: " + items(i))

 6     case lag => println("lag: " + items(i))

 7     case _ =>

 8   }

 9   i = i + 1

10 }

 
次のように表示されます.http://stackoverflow.com/questions/7078022/why-does-pattern-matching-in-scala-not-work-with-variablesScalaでは,matchはstable identifierでなければならないので,不明であるため,後で検討する.次のように変更します.
 1 var i: Int = 0

 2 while (i < items.length) {

 3   i % width match {

 4     case `offset` => println("offset: " + items(i))

 5     case `logSize` => println("logSize: " + items(i))

 6     case `lag` => println("lag: " + items(i))

 7     case _ =>

 8   }

 9   i = i + 1

10 }