Inferring static array size
rkompass
rkompass at gmx.de
Sat May 4 07:17:49 UTC 2024
On Friday, 3 May 2024 at 18:27:56 UTC, Nick Treleaven wrote:
>
> `staticArray!range` only works when `range` elements are known
> at compile-time.
>
> ```d
> auto powersOfTwo = staticArray!10(generate!(() => i *= 2)());
> pragma(msg, typeof(powersOfTwo)); // int[10]
> ```
Thanks for the many nice explanations. I now have the feeling I
get the point.
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?
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?
3. Would it perhaps be possible to code a better `iota()` that
has this ability?
These are learners questions.
I hope you don't mind. They belong to this topic, I suppose.
Thank you.
More information about the dip.ideas
mailing list