Re: switch/case syntax
- Posted by Matt Lewis <matthewwalkerlewis at gmail.c??> Jun 06, 2008
- 608 views
Fernando Bauer wrote: > > Some questions: > 1) Why 'case else:' and not only 'else:'? > 2) Is this possible: 'case {1,2,3}:'? > 3) Is this possible: 'case s:' where s is a sequence variable? > 4) Is it faster than the equivalent code with 'elsif'? > 5) Is it possible to have more 'case's after the 'case else:'? 1) It follows the case paradigm. I suppose it could be simply else. 2) No. The way it is implemented, the only values that work for a case are atoms and strings. Basically, things for which the parser can determine the value for at compile time. 3) If s is a constant, with a simple definition (i.e., not an expression, or the result of a function call), then yes. 4) We haven't done much testing yet, but I recorded the token stream in the parser from parsing int.ex, and tested with the if-elsif vs case, and the case was about 50% faster. 5) No. Here's why some of the above matters. The implementation utilizes euphoria's built in find capability. At compile time, it creates a sequence of all the cases, as well as a jump table. It uses find to see which case (if any) applies, and jumps to that case, or the else, if there is no match. It would be possible to build a case statement that could use arbitrary expressions, but it would be a lot slower, because you'd have to evaluate them every time it happened, before you could do the evaluation to determine the jump. Matt