Jump to page: 1 2 3
Thread overview
What is the use case for this weird switch mecanism
Oct 30, 2012
deadalnix
Oct 30, 2012
H. S. Teoh
Oct 30, 2012
Philippe Sigaud
Oct 30, 2012
deadalnix
Oct 30, 2012
bearophile
Oct 30, 2012
Andrej Mitrovic
Oct 30, 2012
deadalnix
Oct 30, 2012
Nick Sabalausky
Oct 30, 2012
bearophile
Oct 30, 2012
Era Scarecrow
Oct 30, 2012
bearophile
Oct 30, 2012
Era Scarecrow
Oct 30, 2012
H. S. Teoh
Oct 30, 2012
Daniel Murphy
Oct 31, 2012
bearophile
Oct 31, 2012
Era Scarecrow
Oct 31, 2012
bearophile
Oct 31, 2012
Era Scarecrow
Nov 05, 2012
bearophile
Nov 05, 2012
deadalnix
October 30, 2012
Today, I noticed by digging into D details the following construct :

switch(foo) {
    statement;
    case A:
        // Stuffs . . .

    // Other cases.
    default:
        // Stuffs . . .
}

What the hell statement is supposed to do ? And what is the use case for this ?
October 30, 2012
On Tue, Oct 30, 2012 at 05:16:48PM +0100, deadalnix wrote:
> Today, I noticed by digging into D details the following construct :
> 
> switch(foo) {
>     statement;
>     case A:
>         // Stuffs . . .
> 
>     // Other cases.
>     default:
>         // Stuffs . . .
> }
> 
> What the hell statement is supposed to do ? And what is the use case for this ?

That's weird. I just did a quick test; apparently statement is never run. I've no idea why it's allowed or what it's for.


T

-- 
Debian GNU/Linux: Cray on your desktop.
October 30, 2012
>> What the hell statement is supposed to do ? And what is the use case for this ?
>
> That's weird. I just did a quick test; apparently statement is never run. I've no idea why it's allowed or what it's for.

I've no idea why it's authorized, but it saved my day a week ago, in an automatically-generated switch statement that happened to have a "return true;" inserted at the very beginning. No unit test found that and I saw it only by printing the generated code for another search.

In a way, it's logical: the code path jumps to the matching case, so it never sees the first statement block before the first case.
October 30, 2012
deadalnix:

> What the hell statement is supposed to do ? And what is the use case for this ?

See also this bug report I've opened time ago:
http://d.puremagic.com/issues/show_bug.cgi?id=3820

Bye,
bearophile
October 30, 2012
On 10/30/12, Philippe Sigaud <philippe.sigaud@gmail.com> wrote:
> I've no idea why it's authorized

There could be a label for a goto there.
October 30, 2012
Le 30/10/2012 18:47, Philippe Sigaud a écrit :
>>> What the hell statement is supposed to do ? And what is the use case
>>> for this ?
>>
>> That's weird. I just did a quick test; apparently statement is never
>> run. I've no idea why it's allowed or what it's for.
>
> I've no idea why it's authorized, but it saved my day a week ago, in
> an automatically-generated switch statement that happened to have a
> "return true;" inserted at the very beginning. No unit test found that
> and I saw it only by printing the generated code for another search.
>
> In a way, it's logical: the code path jumps to the matching case, so
> it never sees the first statement block before the first case.

I usually want to avoid code working in an unexpected way. Even when it make code work when I expect it shouldn't.

I wrote about this publicly few mounth ago, and, considering how much return I got, I'm not the only one.
October 30, 2012
Le 30/10/2012 18:57, Andrej Mitrovic a écrit :
> On 10/30/12, Philippe Sigaud<philippe.sigaud@gmail.com>  wrote:
>> I've no idea why it's authorized
>
> There could be a label for a goto there.

That still don't explain what the use case is.
October 30, 2012
On Tue, 30 Oct 2012 21:39:31 +0100
deadalnix <deadalnix@gmail.com> wrote:

> Le 30/10/2012 18:57, Andrej Mitrovic a écrit :
> > On 10/30/12, Philippe Sigaud<philippe.sigaud@gmail.com>  wrote:
> >> I've no idea why it's authorized
> >
> > There could be a label for a goto there.
> 
> That still don't explain what the use case is.

Obfuscated coding contests?

October 30, 2012
Nick Sabalausky:

> Obfuscated coding contests?

It's there to help programmers create more bugs, of course :o)

Bye,
bearophile
October 30, 2012
On Tuesday, 30 October 2012 at 21:11:57 UTC, bearophile wrote:
> Nick Sabalausky:
>
>> Obfuscated coding contests?
>
> It's there to help programmers create more bugs, of course :o)

 Maybe variable declaration (as long as they are default(s))? Has a certain amount of sense, but makes more sense to do it outside the switch case...
« First   ‹ Prev
1 2 3