View mode: basic / threaded / horizontal-split · Log in · Help
October 30, 2012
What is the use case for this weird switch mecanism
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
Re: What is the use case for this weird switch mecanism
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
Re: What is the use case for this weird switch mecanism
>> 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
Re: What is the use case for this weird switch mecanism
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
Re: What is the use case for this weird switch mecanism
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
Re: What is the use case for this weird switch mecanism
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
Re: What is the use case for this weird switch mecanism
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
Re: What is the use case for this weird switch mecanism
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
Re: What is the use case for this weird switch mecanism
Nick Sabalausky:

> Obfuscated coding contests?

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

Bye,
bearophile
October 30, 2012
Re: What is the use case for this weird switch mecanism
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
Top | Discussion index | About this forum | D home