Reopening the debate about non-nullable-by-default: initialization of member fields

bearophile via Digitalmars-d digitalmars-d at puremagic.com
Fri May 2 17:58:51 PDT 2014


Idan Arye:

> today I've seen a question in Stack Exchange's Programmers 
> board that raises a point I don't recall being discussed here:

This program:

class A {
     immutable int x;
     this() {
         foo();
         x = 1;
         x = 2;
     }
     void foo() {
         auto y = x;
     }
}
void main() {}


Gives:

temp.d(6,9): Error: immutable field 'x' initialized multiple times

So D can tell x is initialized more than 1 time, but it can't 
tell x is initialized 0 times inside foo().

Bye,
bearophile


More information about the Digitalmars-d mailing list