Re: SWITCH question
- Posted by irv Jan 05, 2018
- 1964 views
It appears that the docs are slightly misleading:
4.4.2 Switch statement The <val> in a case must be either an atom, literal string, constant or enum...
atom expr = 1 atom val = x -- * switch expr do case val then puts(1, "\n result is first stack \n" ) case else puts(1, "\n result is something else \n") end switch
<0091>:: found variable but expected 'else', an atom, string, constant or enum
case val then
So, val must be declared as a constant, or an enum. Otherwise, the val must be literal. e.g:
case 1 then ... case 1.23 then ... case "Five" then ..
IOW, val must never be anything that is 'changeable'.
Secondly, switch() is not exactly comparable to a if..then..else statement, since if..then can deal with variables as the val,(including changing them within the statement).
Perhaps the docs could be improved to make this distinction clearer, as well as emphasizing the fact that you should never use duplicate switch cases. Apparently, I haven't run across that particular problem in real life, because I'm too lazy to type the same case ... twice.