Docs: Section on local variables

Nick Sabalausky SeeWebsiteToContactMe at semitwist.com
Sun Apr 22 01:23:05 PDT 2012


"Andrej Mitrovic" <andrej.mitrovich at gmail.com> wrote in message 
news:mailman.2024.1335025462.4860.digitalmars-d at puremagic.com...
> On 4/21/12, Nick Sabalausky <SeeWebsiteToContactMe at semitwist.com> wrote:
>> Yes, it's initialized to .init by default. But .init may not be what the
>> programmer intended it to be initialized to.
>
> Well, we could introduce a new language feature that explicitly makes
> the compiler verify that you've initialized a variable before using
> it. This could help in cases where e.g. you have static if sections
> and you forget to add an else clause:
>
> string name = none;  // 'none' would be a sentinel for the compiler
> enum i = 2;
> static if (i == 1) {
>    name = ...;
> }
> // forgotten else clause
> if (name == ...) { }  // compile-time error
>
> This way we don't interrupt existing code that relies on .init.

Hmm, it's technically possible. But if you have to *remember* to tell the 
compiler to make sure you've remembered to init a value, then I think we've 
probably blown way past the point of diminishing returns.

> Either
> we introduce some new syntax for initializers, or we get customized
> warning options. Personally I prefer the latter,

Yea, same here.

> but unfortunately
> Walter went full-on Steve Jobs on compiler options for some reason.

Heh heh :)

>
> Also take note of this case:
> int[string] hash;
> ...
> int x = hash.get("foo", 0);
>
> I think this is perfectly legal code that shouldn't be worthy of a
> warning. But how can the compiler know not to emit a warning here?

That's an interesting case. I don't know how that should be handled.




More information about the Digitalmars-d mailing list