Inferring static array size
NotYouAgain
NotYouAgain at gmail.com
Fri May 3 08:11:06 UTC 2024
On Friday, 3 May 2024 at 07:48:01 UTC, rkompass wrote:
>
> I actually find this counterintuitive. Because [..] in my
> intuition looks like a dynamic array. But it's about creating a
> static array.
> ...
[..] is a slice thing - it's not related to how the array came
into existence.
i.e it can work on a static array, and a dynamic array (see code
example below).
Perhaps for that reason, [..] might not be suitable after all.
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.
}
More information about the dip.ideas
mailing list