Fixing C's Biggest Mistake

Patrick Schluter Patrick.Schluter at bbox.fr
Fri Jan 6 13:43:33 UTC 2023


On Thursday, 5 January 2023 at 23:09:43 UTC, H. S. Teoh wrote:
> On Thu, Jan 05, 2023 at 10:35:37PM +0000, areYouSureAboutThat 
> via Digitalmars-d wrote: [...]
>> to be honest, I've never needed a static array in D.
>
> :-D  I haven't really used static arrays very much myself, but 
> occasionally they're useful.  E.g. in one project where I use a 
> lot of 4-element int arrays, using static arrays for them 
> reduces a lot of GC pressure, and also gives me nice by-value 
> semantics (useful in certain contexts).
>
>
> [...]
>> in the meantime.. this is hardly .. hard:
>> 
>> auto myStaticArray = [0, 1].staticArray;
>> 
>> int[2] myStaticArray = [0, 1].staticArray;
>> 
>> people can still count.. can't they?
>
> It's not so much about counting, it's about maintainability / 
> mutability of the code.  For example, if you had a long static 
> array like this:
>
> 	int[100] x = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
> 		43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101,
> 		103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157,
> 		163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223,
> 		227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277,
> 		281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
> 		353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419,
> 		421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479,
> 		487, 491, 499, 503, 509, 521, 523, 541 ];
>
> if one day you decide that some elements have to be 
> added/removed from this array (based on some arbitrary 
> criteria), then you have to recount and update the length, 
> which, for a long array, isn't a trivial effort. It's also not 
> very DRY; the compiler can already figure this out for you, so 
> why make the programmer repeat the same work?
>
> Note that static arrays isn't just arrays of numbers, it could 
> potentially be an array of some aggregate type with complex 
> initializers that makes it annoying to have to recount every 
> time you update it.

The annoying and shameful thing about this static array size 
auto-determination is that it is a feature that even K&R C was 
able to provide. We will probably see men on the Moon again 
before D is able to do it ;-)



More information about the Digitalmars-d mailing list