View mode: basic / threaded / horizontal-split · Log in · Help
December 31, 2008
Improvement to switch-case statement
Just an idea that could improve Ds switch-case statement - add an elipsys as in object Pascal to allow case ranges.

switch (var) {
   case 1:
       \\ do something
       break;
   case 2..10:
       \\ do something else
       break;
}

What do people think?

-=mike=-
December 31, 2008
Re: Improvement to switch-case statement
On Wed, Dec 31, 2008 at 5:16 PM, Mike James <foo@bar.com> wrote:
> Just an idea that could improve Ds switch-case statement - add an elipsys as in object Pascal to allow case ranges.
>
> switch (var) {
>    case 1:
>        \\ do something
>        break;
>    case 2..10:
>        \\ do something else
>        break;
> }
>
> What do people think?

It's such a common need, and such a simple thing to implement, that
I'm absolutely shocked that more languages _haven't_ adopted it.  It's
annoying when you're writing some kind of string parser and have to
put all sorts of stuff in the 'default' that could easily be handled
by cases like this.

In fact, I'll put it in MiniD right now.  Geez.
December 31, 2008
Re: Improvement to switch-case statement
Mike James wrote:
> Just an idea that could improve Ds switch-case statement - add an elipsys as in object Pascal to allow case ranges.
>
> switch (var) {
>      case 1:
>          \\ do something
>          break;
>      case 2..10:
>          \\ do something else
>          break;
> }
>
> What do people think?
>
> -=mike=-

vote--

IMO, either the switch statement should remain the low level 
(effiecient) construct as in C, or replaced by a general mechanism of 
pattern matching. your solution is kinda between the two above options 
and I don't see the point of it.

I'd personally prefer pattern matching to be added to D and the switch 
to be deprecated and eventually removed from the language.
December 31, 2008
Re: Improvement to switch-case statement
On Thu, Jan 01, 2009 at 12:45:39AM +0200, Yigal Chripun wrote:
> IMO, either the switch statement should remain the low level 
> (effiecient) construct as in C, or replaced by a general mechanism of 
> pattern matching. 

This wouldn't be any less efficient than any other switch statement; writing

case 2..5:
 code;
 break;

could just be magically converted into

case 2:
case 3:
case 4:
case 5:
 code;
 break;

That's not ideal when doing a huge range; I'd probably write it like so in asm:

cmp ax, 2
je code
cmp ax, 3
je code
cmp ax, 4
je code
cmp ax, 5
je code
; more cases....
jmp default
code:
 ; the code from inside the case goes here

Doing a bigger range could let you write something more like this:

cmp ax, 2
jl more_cases
cmp ax, 5
jg more_cases
; case 2..5 code here
more_cases:
; check other cases
jmp default

Or maybe:

cmp ax, 2
jge code
more_cases:
; more cases....
jmp default
code:
cmp ax, 5
jg more_cases
; proceed with the code inside


If I was writing it by hand, I'd probably pick one based on what other cases 
there are to try and minimize the jumps. The compiler could surely do the same.


Anyway though, the point is the simple range in the case is at least no
worse than writing it in C with a series of cases, and might be better, since
the compiler can condense it all down into just a few instructions rather
than a big laundry list.



Finally, the D switch statement already does strings as cases, which the
one in C would never do; the D switch already has moved beyond it, so it
wouldn't be out of character for it to pick up the ranges too.

-- 
Adam D. Ruppe
http://arsdnet.net
December 31, 2008
Re: Improvement to switch-case statement
Hello Yigal,

> Mike James wrote:
> 
>> Just an idea that could improve Ds switch-case statement - add an
>> elipsys as in object Pascal to allow case ranges.
>> 
>> switch (var) {
>> case 1:
>> \\ do something
>> break;
>> case 2..10:
>> \\ do something else
>> break;
>> }
>> What do people think?
>> 
>> -=mike=-
>> 
> vote--
> 
> IMO, either the switch statement should remain the low level
> (effiecient) construct as in C, or replaced by a general mechanism of
> pattern matching. your solution is kinda between the two above options
> and I don't see the point of it.
> 
> I'd personally prefer pattern matching to be added to D and the switch
> to be deprecated and eventually removed from the language.
> 


Well, the D switch statement has already abandoned the low-level C construct 
in that it allows for switching on strings.  I don't think there is a reason 
to believe that adding the range feature would mean a loss of efficiency.

I have to agree that this would appear to be a fairly simple and useful feature 
that exists in other "old" languages like Pascal and derivitives.  I'm not 
sure why it was left out in D, but I wouldn't be a bit surprised if the request 
has been made before.

On the other hand, it probably wouldn't be hard to implement a range as a 
compile time template (or plain) function inserted after the case statement:

case range(2,10): //

But then again... since array slices make use of "..", it seems a shame that 
it can't also be used in the case statement for a similar purpose.

-JJR
December 31, 2008
Re: Improvement to switch-case statement
> 
> case range(2,10): //
> 

Er... pardon... I don't think that would work with CTFI. 

I was thinking too quickly on that one. :P

-JJR
December 31, 2008
Re: Improvement to switch-case statement
John Reimer wrote:
> Hello Yigal,
>
>> Mike James wrote:
>>
>>> Just an idea that could improve Ds switch-case statement - add an
>>> elipsys as in object Pascal to allow case ranges.
>>>
>>> switch (var) {
>>> case 1:
>>> \\ do something
>>> break;
>>> case 2..10:
>>> \\ do something else
>>> break;
>>> }
>>> What do people think?
>>>
>>> -=mike=-
>>>
>> vote--
>>
>> IMO, either the switch statement should remain the low level
>> (effiecient) construct as in C, or replaced by a general mechanism of
>> pattern matching. your solution is kinda between the two above options
>> and I don't see the point of it.
>>
>> I'd personally prefer pattern matching to be added to D and the switch
>> to be deprecated and eventually removed from the language.
>>
>
>
> Well, the D switch statement has already abandoned the low-level C
> construct in that it allows for switching on strings. I don't think
> there is a reason to believe that adding the range feature would mean a
> loss of efficiency.
>
> I have to agree that this would appear to be a fairly simple and useful
> feature that exists in other "old" languages like Pascal and
> derivitives. I'm not sure why it was left out in D, but I wouldn't be a
> bit surprised if the request has been made before.
>
> On the other hand, it probably wouldn't be hard to implement a range as
> a compile time template (or plain) function inserted after the case
> statement:
>
> case range(2,10): //
>
> But then again... since array slices make use of "..", it seems a shame
> that it can't also be used in the case statement for a similar purpose.
>
> -JJR
>
>

D's switch is indeed better than C already but it still is rather 
low-level at least from a syntax point of view: it uses labels and break.
Can the current switch be extended to support full pattern matching 
without conflicting with the current syntax?
what about specifying alist of values, like: case 1,2,3:...
January 01, 2009
Re: Improvement to switch-case statement
Adam D. Ruppe:

> case 2..5:
>   code;
>   break;
> 
> could just be magically converted into
> case 2:
> case 3:
> case 4:
> case 5:
>   code;
>   break;

Note that the following syntax is already possible in D:

case 2,3,4,5:
 code;
 break;

But the range gets larger that syntax gets ugly, so the .. range looks like a good thing to have. So I'm +1 on the idea of the original poster.

The range syntax of the D2 foreach may be even extended to support:

if (x in 10..20) {...

That more or less equals to the C macro:
#define InRange(x, a, b) (unsigned((x) - (a)) <= (b) - (a))

That as you can see has just one branch instead of the usual two:
http://smallcode.weblogs.us/checking_if_point_belongs_to_interval

Bye,
bearophile
January 01, 2009
Re: Improvement to switch-case statement
On 2008-12-31 18:22:32 -0500, "Adam D. Ruppe" <destructionator@gmail.com> said:

> This wouldn't be any less efficient than any other switch statement; writing
> 
> case 2..5:
>   code;
>   break;
> 
> could just be magically converted into
> 
> case 2:
> case 3:
> case 4:
> case 5:
>   code;
>   break;

Except that if you want to keep consistency with array slices, 2..5 
should probably not include 5 in the range, thus should be equivalent 
"case 2,3,4". Unfortunatly, that's generally not what you want when 
creating such cases. Imagine writting:

	case 'a'..'{':
	case 'A'..'[':

Concistent, yes; but not very appealing. What you want to be able to 
write is this:

	case 'a'..'z':
	case 'A'..'Z':

But then it doesn't work as elsewhere in the language anymore.

-- 
Michel Fortin
michel.fortin@michelf.com
http://michelf.com/
January 01, 2009
Re: Improvement to switch-case statement
Hello Yigal,

> John Reimer wrote:
> 
>> Hello Yigal,
>> 
>>> Mike James wrote:
>>> 
>>>> Just an idea that could improve Ds switch-case statement - add an
>>>> elipsys as in object Pascal to allow case ranges.
>>>> 
>>>> switch (var) {
>>>> case 1:
>>>> \\ do something
>>>> break;
>>>> case 2..10:
>>>> \\ do something else
>>>> break;
>>>> }
>>>> What do people think?
>>>> -=mike=-
>>>> 
>>> vote--
>>> 
>>> IMO, either the switch statement should remain the low level
>>> (effiecient) construct as in C, or replaced by a general mechanism
>>> of pattern matching. your solution is kinda between the two above
>>> options and I don't see the point of it.
>>> 
>>> I'd personally prefer pattern matching to be added to D and the
>>> switch to be deprecated and eventually removed from the language.
>>> 
>> Well, the D switch statement has already abandoned the low-level C
>> construct in that it allows for switching on strings. I don't think
>> there is a reason to believe that adding the range feature would mean
>> a loss of efficiency.
>> 
>> I have to agree that this would appear to be a fairly simple and
>> useful feature that exists in other "old" languages like Pascal and
>> derivitives. I'm not sure why it was left out in D, but I wouldn't be
>> a bit surprised if the request has been made before.
>> 
>> On the other hand, it probably wouldn't be hard to implement a range
>> as a compile time template (or plain) function inserted after the
>> case statement:
>> 
>> case range(2,10): //
>> 
>> But then again... since array slices make use of "..", it seems a
>> shame that it can't also be used in the case statement for a similar
>> purpose.
>> 
>> -JJR
>> 
> D's switch is indeed better than C already but it still is rather
> low-level at least from a syntax point of view: it uses labels and
> break.
> Can the current switch be extended to support full pattern matching
> without conflicting with the current syntax?
> what about specifying alist of values, like: case 1,2,3:...


True, a case statment can take a comma-separated expression list like that, 
so maybe there is an opportunity for a compile-time template mixin or something 
afterall.   I forgot about the comma op.

That's makes things a little more promising. 

-JJR
« First   ‹ Prev
1 2 3 4
Top | Discussion index | About this forum | D home