Inferring static array size

Chloé chloekek at use.startmail.com
Sun Apr 28 09:35:22 UTC 2024


On 4/28/24 11:31, NotYouAgain wrote:
> The idea is simple, really, really simple.
> 
> Instead of this:
> 
>   int[5] a = [1, 2, 3, 4, 5];
> 
> allow this:
> 
>   int[$_] a = [1, 2, 3, 4, 5];
> 
> Both are static arrays.
> 
> Having to change the length everytime i change the initialisation list 
> is a pain in the $____
> 
> The compiler infers the size in the second example, based on the number 
> of elements in the initialisation list. Just like C can do.
> 

The Phobos function staticArray can be used to infer the array length 
while explicitly specifying (or inferring) the element type:

     import std.array : staticArray;
     auto a = [1, 2, 3, 4, 5].staticArray;  // inferred int[5]
     auto b = [1, 2, 3, 4, 5].staticArray!float;  // inferred float[5]



More information about the dip.ideas mailing list