www.digitalmars.com         C & C++   DMDScript  

digitalmars.dip.ideas - Missing Elements in Static Array Initializers

reply Nick Treleaven <nick geany.org> writes:
I've written up a formal DIP which needs discussion in this forum 
first:
https://github.com/ntrel/DIPs/blob/6678d4ef9d233dd5780b80900debdf7066165655/DIPs/1NNN-NMT.md#missing-elements-in-static-array-initializers

Initially I also included a proposal for overriding `E.init` for 
missing elements (with syntax `[elements, ... = init]`) as I saw 
Walter propose syntax for that in the September meeting write-up:
https://forum.dlang.org/post/ucuhbblifjcjkfvikbqm forum.dlang.org

However I think that part is less compelling so I've removed it 
from the DIP above. Part of the justification for it is to make 
it easier to port C code which requires zero initialization. 
However, the above DIP suggests that the compiler warns the user 
about this when `E.init` is nonzero. The user would then need to 
set the missing elements to zero at runtime.
Mar 25
next sibling parent Walter Bright <newshound2 digitalmars.com> writes:
Thank you, Nick!

The rationale for accepting too few elements and default initializing the rest 
comes from:

```d
int array[1000] = [1,2,3];
```

where it is simply impractical to write out 997 initializers. This pattern 
occurs in C.

I like the proposed `[1,2,3,...]` syntax a lot better than the "Alternatives"
ideas.
Apr 03
prev sibling parent Quirin Schroll <qs.il.paperinik gmail.com> writes:
On Wednesday, 25 March 2026 at 15:45:29 UTC, Nick Treleaven wrote:
 I've written up a formal DIP which needs discussion in this 
 forum first:
 https://github.com/ntrel/DIPs/blob/6678d4ef9d233dd5780b80900debdf7066165655/DIPs/1NNN-NMT.md#missing-elements-in-static-array-initializers

 Initially I also included a proposal for overriding `E.init` 
 for missing elements (with syntax `[elements, ... = init]`) as 
 I saw Walter propose syntax for that in the September meeting 
 write-up:
 https://forum.dlang.org/post/ucuhbblifjcjkfvikbqm forum.dlang.org

 However I think that part is less compelling so I've removed it 
 from the DIP above. Part of the justification for it is to make 
 it easier to port C code which requires zero initialization. 
 However, the above DIP suggests that the compiler warns the 
 user about this when `E.init` is nonzero. The user would then 
 need to set the missing elements to zero at runtime.
I disagree with two restrictions:
 Static initialization from an array initializer with missing 
 elements will be deprecated in the next edition, except when:
 * […]
 * […]
 * the array initializer is empty `[]`
and
 It is an error to use [elements, ...] initializer syntax when:
 * […]
 * there are no missing elements
The first one adds a special rule which I don’t think is justified. There’s no big drawback to writing `[...]`. Only for the current Edition, I’d not make this requirement because it’s needlessly breaking. Moving forward, `[]` should only initialize empty arrays, and `[...]` initializes arrays of any size, filling them with `init`. The second one means `...` must produce one or more elements, when it’s rather obvious it can also produce none. It should be allowed to do so, otherwise meta-programming might require special handling to omit `...` when the number of elements and the desired size happen to line up. By far the easiest fix is to just allow `...` even if it inserts no elements because enough of them are in the initializer.
Apr 10