May 30, 2023 static immutable that has no initialiser - should this raise an error? | ||||
---|---|---|---|---|
| ||||
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 31, 2023 Re: static immutable that has no initialiser - should this raise an error? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Cecil Ward | 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.
|
Copyright © 1999-2021 by the D Language Foundation