Re: switch statement
- Posted by Kat Mar 25, 2009
- 1362 views
mattlewis said...
Kat said...
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.
Would it be faster to eval() when and only when () are encountered, or code like this:
case 1:
case 2:
case 3:
vs
case (x<4):
useless