Switch - jpbubble/NIL-isn-t-Lua GitHub Wiki
Yes, folks, unlike Lua, NIL DOES support a switch statement. And it's quite easy to use.
for i=1,12
// single case
switch i
case 1
print("I")
case 2
print("II")
case 3
print("III")
case 4
print("IV")
case 5
print("V")
case 6
print("VI")
case 7
print("VII")
case 8
print("VIII")
case 9
print("IX")
case 10
print("X")
case 11
print("XI")
case 12
print("X")
end
// Multi case
switch i
case 1 2 3
print("Quarter 1")
case 4 5 6
print("Quarter 2")
case 7 8 9
print("Quarter 3")
case 10 11 12
print("Quarter 4")
end
// With Default
switch i
case 3 6 9 12
print("***")
default
print(" ")
end
end
See how easy it works. There are some notes to make though.
- switch itself and all case and default statements need a line for themselves alone
- In multi-cases, no commas or anything needed.
- Cases can only contain constant values, making them only compatible with strings, numbers and booleans. Please note that macros can be used for this, as they are substituted before NIL even knows there is a case.
- I guess this one is very important to C coders (or to those who coded in C-syntax languages), fallthroughs are not possible in any kind of way. I am not planning to introduce them either on a short notice either, due to them not being possible, in the current translation, method. And if they are ever added in the future, they will be done on the method "Go" uses... So not needing a "break" all the time, but the keyword "fallthrough" in stead. But as I said, not something to be expected on short term.