New switch/case idea
- Posted by Fernando Mar 30, 2009
- 1890 views
Hi switchers ,
Another idea:
Rather than modify 'switch', how about modify 'case' to:
'case' [<case_modifier>] <valuelist> [':' | 'then']
Suppose 'case' without modifier means without fall-through, then we could have a modifier to force fall-through:
For instance, let the keyword continue (to not add a new keyword) represents fall-through, then we could have something like this (based on Jeremy's example):
switch 1 do case 1: ? 1 case 2: ? 2 end switch -- Output: -- 1
and
switch 1 do case continue 1: ? 1 case continue 2: ? 2 case 3: ? 3 case 4: ? 4 end switch -- Output: -- 1 -- 2 -- 3
Features:
- It doesn't add a new keyword or word;
- It permits to mix with fall-through and without fall-through cases in the same switch;
- It is easy to implement;
In fact, one <case_modifier> already exists: 'else' (for the default case).
In the future, more <case_modifiers> could be included, for example: 'in', 'range', ...
- Fernando