Top 5

Bruce Adams tortoise_74 at yeah.who.co.uk
Thu Oct 9 15:14:54 PDT 2008


On Thu, 09 Oct 2008 01:08:54 +0100, Chris R. Miller  
<lordsauronthegreat at gmail.com> wrote:

> Mike wrote:
>> 4. Replace C-style switch with something modern and more D-like:
>>  switch (x)
>> {
>>     case (0) foo();
>>     case (1)
>>     {
>>         bar();
>>         baz();
>>     }
>>     else throw new Exception("nope");
>> }
>
> But that would break the techniques of a duff's device.  The existing  
> syntax of a switch statement is more accurate to how it behaves,  
> anyways.  It's like a more complex series of goto statements.
>
> http://en.wikipedia.org/wiki/Duff%27s_device

Duff's device is a perversion. The compiler should be left to perform
those sorts of optimisations.

> http://en.wikipedia.org/wiki/Switch_statement
>
> Your proposal really makes it redundant with chains of if-else  
> statements IMHO.

The difference is a switch might be optimised into a look-up table.
A chain of if/then/else's requires a different kind of optimisation
thought it might end up in the same form.

Though the main point is nothing to do with optimisation. Its about clarity
of intent. A switch selects possible values of a single item and can
be expected (in certain cases) to cover all cases. That is never true for
if-then-else.



More information about the Digitalmars-d mailing list