1. switch/case syntax
Derek Parnell wrote:
>
> Euler German wrote:
>
> > Just to add that there are times when I really miss a DO CASE case
> > this ... case that ... other[wise] ... END CASE construct. But I can
> > wait. :)
>
>
> This construct has been implemented in v4.0 and I'm doing some extensive
> testing
> with it. It works very well and makes code look a lot cleaner.
> eg.
>
> switch upper(SOMEVAL) do
> case "ME":
> case "MYSELF":
> case "I":
> foo()
> exit
>
> case "YOU":
> case "THEM":
> bar()
> -- fall thru
>
> case "HE":
> case "SHE":
> qwerty()
> exit
>
> case else:
> default()
>
> end switch
>
>
> --
> Derek Parnell
> Melbourne, Australia
> Skype name: derek.j.parnell
Cool!!
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:'?
Thanks in advance,
Fernando
2. Re: switch/case syntax
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
3. Re: switch/case syntax
Matt Lewis wrote:
> 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.
Now that's what I'm talking about!! :)