shouldn't this code at least trigger a warning?

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Apr 29 07:57:07 PDT 2015


On Wed, Apr 29, 2015 at 06:37:44AM +0000, ketmar via Digitalmars-d-learn 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".

Switch statements in D allow all sorts of abominations, if only you
would try it. I think it was originally designed to support a particular
loop idiom (sorry I forgot what it was called, and don't have time to
look it up right now), but in the process this also opened the door to
all sorts of nasty infelicities that probably breaks the type system,
control flow, and many other things. Basically, the block inside a
switch statement essentially amounts to free-for-all spaghetti code
where you're free to jump around case labels willy-nilly, declare
variables and jump over their initializations, break out of loops with
goto case, or enter into the middle of a loop, and all sorts of other
crazy things that, ostensibly, you shouldn't be able to do in a language
like D.


T

-- 
Let's eat some disquits while we format the biskettes.


More information about the Digitalmars-d-learn mailing list