Inferring static array size

rkompass rkompass at gmx.de
Fri May 3 09:15:02 UTC 2024


> perhaps [$] is still the best, since $ *always* simply refers 
> to the length of the array.
>
> so:
>
> int[4] // 4 is the length
> int[$] // $ is still the length, you just don't know what it is 
> yet.
>
> module m;
> @safe:
> private:
> import std;
>
> void main()
> {
>     auto a = [1, 2, 3, 4, 5].staticArray;
>     //auto a[$] = [1, 2, 3, 4, 5]; // the compiler can infer 
> the 'length'
>
>     writeln(fold!((a, b) => a + b)(a[0..2])); // 3  (see: [..] 
> used on a static array.
> }

This, of course, is even better. And now we are exactly at
DIP1039: 
https://forum.dlang.org/thread/ucqyqkvaznbxkasvdjpx@forum.dlang.org?page=1

I read through the discusion there and found it quite digressing 
and not productive.
There was no real weighing the pros against the cons.

Of course we have staticArray, but in programming it's much about 
conciseness which at the same time allows for effortlessy 
(intuitively) transporting the meaning of a construct.
And this criterion here is fulfilled.
No new symbol is introduced, just a meaning extended, which is 
immediatly clear.

Could DIP 1039 be restarted?


More information about the dip.ideas mailing list