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 reply 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
parent Nick Treleaven <nick geany.org> writes:
On Friday, 10 April 2026 at 14:56:15 UTC, Quirin Schroll wrote:
 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`.
OK. I think Dennis allowed `[]` for a PR that didn't require an edition, and the test suite uses that. We can start off strict, and see if we get complaints, or if buildkite projects break when the next edition becomes the default. I'm planning on making dscanner auto-fix code when its parser can tell there's a static array declaration with missing elements.
 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.
OK, accepted. Thanks for your review, sorry for the delay!
May 06