www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - static immutable that has no initialiser - should this raise an error?

reply Cecil Ward <cecil cecilward.com> writes:
static immutable T foo;

T bar() {
     return foo;
     }

Should we get an error from the D compiler here as the 
initialiser has been forgotten? What do you think ?

I realise that foo is initialised to a defined value according to 
the type T, and this does seem to be the case here because foo is 
placed in readonly data (in the code segment on my x86-64 
machine) but it doesn’t seem like plausible code as foo can’t 
really be used for anything useful. Built this with GDC and 
inspected the generated asm.
May 29 2023
parent Dom DiSc <dominikus scherkl.de> writes:
On Tuesday, 30 May 2023 at 04:11:00 UTC, Cecil Ward wrote:
 static immutable T foo;

 T bar() {
     return foo;
     }

 Should we get an error from the D compiler here as the 
 initialiser has been forgotten? What do you think ?
No. There are no un-initialized values in D. It gets its default value, if no explicit value is given. That's intended, not an error. If you need an un-initialized value, set it =void. Of course, for immutable values that makes no sense at all. But I think, this already gives you an error.
May 31 2023