January 05, 2008
Janice Caron wrote:
> On 1/5/08, BC <notmi_emayl_adreznot@hotmail.com.remove.not> wrote:
>>> The way I see it, the advantage of "switch/case" over "if" is that the compiler might be able to find cool ways to optimize the code (e.g binary search, sparse array lookup, table lookup, whatever). If the compiler can't do that, well then, what's the point of using it at all when "if" is perfectly expressive already?
>> 
>> Do you know if the compilers actually do this?
> 
> Me? Hell no! I don't write compilers. [...] I do know the answer for at least one ancient compiler of old. [...] But that was then and this is now and things are bound to be different.

FWIW I totally agree with you that there is virtual certainty that some
kind of optimisation (jump table, binary search tree, etc.) is actually
used in each of DMD and GDC and more or less any optimising compiler.
Whatever the target machine is, those are bound to be faster than a
linear chain of if-else-ifs (except for extreme cases).

regards, frank
January 05, 2008
On Sat, 05 Jan 2008 11:50:33 -0000, Janice Caron <caron800@googlemail.com> wrote:

> On 1/5/08, BC <notmi_emayl_adreznot@hotmail.com.remove.not> wrote:
>> > The way I see it, the advantage of "switch/case" over "if" is that the
>> > compiler might be able to find cool ways to optimize the code (e.g
>> > binary search, sparse array lookup, table lookup, whatever). If the
>> > compiler can't do that, well then, what's the point of using it at all
>> > when "if" is perfectly expressive already?
>>
>> Do you know if the compilers actually do this?
>
> Me? Hell no! I don't write compilers. But Walter may be able to answer
> that question for D.
>
> I do know the answer for at least one ancient compiler of old. I know
> that back in the days of AmigaDOS, the Amiga compiler used a choice of
> two strategies. It would create a simple O(1) lookup table if all the
> values were contiguous, or nearly contiguous, or an O(log(N)) binary
> search otherwise. But that was then and this is now and things are
> bound to be different.

SAS C++? them were the days...
January 12, 2008
On 2008-01-07 20:48:54 -0500, bearophile <bearophileHUGS@lycos.com> said:

> In the Python community people collect use cases, do a simple frequency count of them, and then usually look for a simple solution able to cover most of them. So we can collect some use cases of the switch. I presume most use cases are covered by a Pascal-like syntax. The other situations are probably covered by putting commas between alternative cases. What other use cases do you people have?

A use case?

I quite fancy GCC's extension which allows you to put ranges in case statements, for instance:

	switch (myChar) {
		case 'a'...'z':
		case 'A'...'Z':
		case '0'...'9':
			// do something.
	}

I think D should have it too. (Also, since we're at it, it's covered by Pascal.)

-- 
Michel Fortin
michel.fortin@michelf.com
http://michelf.com/

January 12, 2008
Michel Fortin wrote:
> On 2008-01-07 20:48:54 -0500, bearophile <bearophileHUGS@lycos.com> said:
> 
>> In the Python community people collect use cases, do a simple frequency count of them, and then usually look for a simple solution able to cover most of them. So we can collect some use cases of the switch. I presume most use cases are covered by a Pascal-like syntax. The other situations are probably covered by putting commas between alternative cases. What other use cases do you people have?
> 
> A use case?
> 
> I quite fancy GCC's extension which allows you to put ranges in case statements, for instance:
> 
>     switch (myChar) {
>         case 'a'...'z':
>         case 'A'...'Z':
>         case '0'...'9':
>             // do something.
>     }
> 
> I think D should have it too. (Also, since we're at it, it's covered by
> Pascal.)
> 

Yes, but it would be 'A' .. 'Z' not 'A' ... 'Z'.
January 12, 2008
On 1/12/08, Robert DaSilva <sp.unit.262+digitalmars@gmail.com> wrote:
> Yes, but it would be 'A' .. 'Z' not 'A' ... 'Z'.

In D parance, that would mean 'A' to 'Y' inclusive, but excluding 'Z'.

(The three dot form could be used to mean "inclusive" though)
January 12, 2008
Janice Caron Wrote:

> On 1/12/08, Robert DaSilva <sp.unit.262+digitalmars@gmail.com> wrote:
> > Yes, but it would be 'A' .. 'Z' not 'A' ... 'Z'.
> 
> In D parance, that would mean 'A' to 'Y' inclusive, but excluding 'Z'.
> 
> (The three dot form could be used to mean "inclusive" though)

Not only;

it would violate the 0..length meaning, and he also failed to recognize the syntax we've been pushing for to distinguish the new switch from the old.

The new one looks like this:

switch(x) {
   case(y) {
  }
  case (z) {
  }
  case (a) {
 }
}

Why?
1) It's more consistent with other structured D statements.
2) It differentiates it from the old way in our minds
3) It allows us to support both ways for a transition period.
4) The {} is syntactically associated with only affecting what's inside, while : is used to mean "everything after here" which is semantically correct.

Regards,
Dan
January 14, 2008
Robert DaSilva wrote:

> Michel Fortin wrote:
>> On 2008-01-07 20:48:54 -0500, bearophile <bearophileHUGS@lycos.com> said:
>> 
>>> In the Python community people collect use cases, do a simple frequency count of them, and then usually look for a simple solution able to cover most of them. So we can collect some use cases of the switch. I presume most use cases are covered by a Pascal-like syntax. The other situations are probably covered by putting commas between alternative cases. What other use cases do you people have?
>> 
>> A use case?
>> 
>> I quite fancy GCC's extension which allows you to put ranges in case statements, for instance:
>> 
>>     switch (myChar) {
>>         case 'a'...'z':
>>         case 'A'...'Z':
>>         case '0'...'9':
>>             // do something.
>>     }
>> 
>> I think D should have it too. (Also, since we're at it, it's covered by
>> Pascal.)
>> 
> 
> Yes, but it would be 'A' .. 'Z' not 'A' ... 'Z'.

That's always bugged me.  Seems too easy for coding errors.
1 2 3
Next ›   Last »