Inferring static array size

Nick Treleaven nick at geany.org
Fri May 3 18:23:06 UTC 2024


On Friday, 3 May 2024 at 11:45:27 UTC, NotYouAgain wrote:
> As it is, CTFE using .staticArray is a useful alternative... 
> unless you're trying to do something like this:
>
> module m;
> @safe:
> private:
>
> import std;
>
> private enum numItems = 10;
>
> class myClass {}
>
> void main()
> {
>     // Object[numItems] objectArr = generate!(() => new 
> myClass())().take(numItems).array;

Note: that works because you are giving the static array the 
length information at compile time.

>     import std.array : staticArray;
>     auto arr = generate!(() => new 
> myClass())().take(numItems).staticArray; // nope
> }

The compiler would not be able to infer the length of the range 
even if the DIP had been accepted. The range produced by `take` 
does not have access to the length at compile-time. This does 
work:

```d
     auto arr = generate!(() => new 
myClass())().staticArray!numItems;
     pragma(msg, typeof(arr)); // myClass[10]
```


More information about the dip.ideas mailing list