Inferring static array size
Nick Treleaven
nick at geany.org
Sat May 4 09:57:05 UTC 2024
On Saturday, 4 May 2024 at 07:17:49 UTC, rkompass wrote:
> I tried and found that:
> ```d
> auto c = 5.iota.staticArray!5; // works
> ```
>
> My interpretation is: `iota()` is a lazy range function
> (forward range??).
> It's laziness implies that the point of termination (and thus
> the length) is not known at the start.
> Which is a requirement for `staticArray`.
>
> Now the questions arise:
>
> 1. Could an alternative to staticArray be coded that stores the
> elements in a dynamic array at compile time and then convert
> that to a static array?
It already supports that:
```d
auto a = staticArray!(iota(5));
pragma(msg, typeof(a)); // int[5]
```
> 2. Would it make sense to add a range type that is lazy, but at
> the same time provides/transports information about its
> length/size?
Maybe.
> 3. Would it perhaps be possible to code a better `iota()` that
> has this ability?
Maybe, although the elements of iota are known at compile-time
too, so you can just do `enum r = iota(5);` and then pass it as a
runtime argument to a function expecting a range.
More information about the dip.ideas
mailing list