Docs: Section on local variables

Andrej Mitrovic andrej.mitrovich at gmail.com
Sat Apr 21 09:23:11 PDT 2012


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. Either
we introduce some new syntax for initializers, or we get customized
warning options. Personally I prefer the latter, but unfortunately
Walter went full-on Steve Jobs on compiler options for some reason.

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?


More information about the Digitalmars-d mailing list