Boo本身並沒有類似 switch-case 語法,但是可以藉著 macro 來做到,Boo extensions這個專案已經寫好了。
由於這個專案沒有釋出二進位碼,所以你需要自己 checkout 並編譯。
編譯以後,用法也很簡單,一看就能懂了。
import Boo.PatternMatching // match 與 case 這兩個 macro 都在這裡面 def getEnglish( i as int ) as string: s = "" match i: case 0: s = "zero" case 1: s = "one" case 2: s = "two" case 3: s = "three" otherwise: s = "unknown" return s l = array( typeof(int), range( 5 ) ) for item in l: print getEnglish( item )
未來 Boo extensions 會包進 Boo 嗎?很難說…Boo extensions 目前仍然很具實驗性…
P.S.
- 編譯Boo extensions前,請下載最新的 Boo,然後解壓縮以後,放到跟 boo-extensions 同一層。再切換到 boo-extensions/extensions 下執行 nant 即可。
- 如果 Boo extensions 無法編譯成功,試著修改 extensions/default.build 將編譯 .Test.dll 的幾個地方註解掉,再次編譯即可。這些 .Test.dll 其實是用不到的。