shouldn't this code at least trigger a warning?

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Apr 29 20:06:03 PDT 2015


On 4/29/15 5:25 AM, "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm at gmx.net>" 
wrote:
> On Wednesday, 29 April 2015 at 06:37:44 UTC, ketmar wrote:
>> subj. the code:
>>
>>   void main () {
>>     import std.stdio;
>>     char ch = '!';
>>     switch (ch) {
>>       int n = 42;
>>       case '!': writeln(n, ": wow!"); break;
>>       default:
>>     }
>>   }
>>
>>
>> i think that such abomination should:
>> 1. be forbidden, or
>> 2. trigger a warning, or
>> 3. execute initializer anyway.
>>
>> currently the code is allowed, no warnings triggered, yet `n` is
>> uninitialized. and having uninitialized variable without "=void" "should
>> not be".
>
> Agreed, this should be an error. Variables declared in one case block
> aren't even visible in other case blocks, this was probably an oversight.

I thought they were? A switch scope is just like a normal scope, but 
with a bunch of labels.

They should be visible in all *trailing* case blocks, since the rule is 
that your code that uses it must come after the declaration.

I agree the above should not be allowed. It's clear the compiler can 
detect a situation where n is used but not initialized.

-Steve


More information about the Digitalmars-d-learn mailing list