Re: switch statement
- Posted by mattlewis (admin) Mar 25, 2009
- 1366 views
I like the switch statement, it's already saved me a tangle of if-statements.
I'd like to make a suggestion:
value = {1,2,3} switch value do case 1: -- code, break case (1+1): -- code runs if value = 2, break case (var - 12): -- code , break case (value < 4): -- if all the elements of value are < 4, break case (match("cow,value)): end switch
Basically, if () are encountered, then call eval() to run the expression in the ().
Future versions of switch may behave this way, or at least support more complex cases (no promises ). One of the motivations of the current switch was to offer a faster and sometimes more expressive construct than the existing if-elsif-else option.
This sort of thing (executable code) would definitely remove a lot of the speed value, though obviously not the expressiveness.
Besides, value is somewhat executeable in a sense, i do this in http.e, and it runs fine:
switch sendheader[idx][1] do
And i believe it executes (to dig into sendheader) because nothing is known about sendheader at compile time except it's declared as a sequence.
Yes, it's not really any different than any other use of a subscript. Basically, it creates a temp as the result of the subscript, and uses that value to direct the flow.
How to make a select switch:
switch value do case a: break case b: break case c: end switch
Think of it as a new two-word keyword. I did not try it. Depending on the switch parser, it may loose it's footing and crash. Basic Eu doesn't care if "break" is on another or the same line, but i don't know where it sees "case" in the context of the stack of testable conditions. I just think of "break" as Pascal's ; eol tag:
No, it doesn't care about line breaks. That's just a different type of white space, as far as the scanner/parser are concerned.
Matt