Euphoria Ticket #764: allow switch case to accept a range and any sequence

hi my suggestion is code like this:

include std/convert.e 
 integer x = rand(50) 
 switch (x) do 
    case 0..20 then 
    case else 
        ? x     
 end switch  

1. where 0..20 creates a range (just a pair,small <= x and x <=large )

2. in addition, it should be possible to create a sequence using code,
- and not just commas -
by calling the function directly between case ... then.


Type: Feature Request Severity: Normal Category: Language
Assigned To: unknown Status: New Reported Release: 4.0.4
Fixed in SVN #: View VCS: none Milestone:

1. Comment by DerekParnell May 02, 2012


The purpose of the switch statement is to perform a fast (efficient) multi-target branch. It is meant to be faster than the equivalent code using if .. elsif ... end if. To make this happen, the potential values to test against need to be known at parsing time rather than at execution time. So while a range of literals is a good idea (we had already earmarked this as a future enhancement), using run-time functions or expressions in the case .. then clause would be contrary to the aims of the switch statement.

And I think the syntax will be more like ...

 integer x = rand(50)  
 switch (x) do  
    case 0 to 9 then -- 0,1,2,3,4,5,6,7,8,9 
    case 21 to 29 by 2 then -- 21,23,25,27,29 
        ? x      
    case 10 to 50 by *10 then -- 10,20,30,40,50 
 end switch   

2. Comment by kobi May 02, 2012

I see, that's a fine syntax. personally, I find the switch syntax clearer (easier to understand at a glance) than nested if elses.. (so for me the purpose is not performance)

what do you think about 'sequence returning' functions, whose results are known at compile time? (the parameters are known, and there are no side effects in the function) maybe it's harder to determine or implement...

3. Comment by DerekParnell May 02, 2012

I'd also like to see CTFE (compile time function evaluation) and pure functions implemented in Euphoria, and that may happen one day. This would make ...

 atom x = power(2,5) 
 atom x = 32 

identical, which may lead to some significant performance improvements, or more legible code (expressing intent rather than just using 'magic' numbers).

A pure function is one that is guaranteed not to have any side-effects outside the function.


Quick Links

User menu

Not signed in.

Misc Menu