當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Scala annotation.switch用法及代碼示例

用法:

final class switch extends StaticAnnotation

要應用於匹配表達式的注釋。如果存在,編譯器將驗證匹配是否已編譯為 tableswitch or lookupswitch,如果它編譯為一係列條件表達式,則會發出警告。示例用法:

val Constant = 'Q'
def tokenMe(ch: Char) = (ch: @switch) match {
  case ' ' | '\t' | '\n'  => 1
  case 'A' | 'Z' | '$'    => 2
  case '5' | Constant     => 3  // a non-literal may prevent switch generation: this would not compile
  case _                  => 4
}

注意:對於一種或兩種情況的模式匹配,編譯器會生成跳轉指令。用@switch 注釋這樣的匹配不會發出任何警告。

源碼:

switch.scala

相關用法


注:本文由純淨天空篩選整理自scala-lang.org大神的英文原創作品 annotation.switch。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。