www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Automatic static array length

reply Your Name <email example.org> writes:
Is there a way to declare a static array with an automatic 
length, so that it doesn't have to be manually calculated?

Perhaps something like this:
int[auto] arr = [12, 23, 54, 653, 2, 6432, 345, 435, 26];

the current way to do it is this:
int[9] arr = [12, 23, 54, 653, 2, 6432, 345, 435, 26];

But then you have to manually count the items, and update the 
number each time the array is changed.

Is there any way to declare a static array with an automatically 
inferred length?
Dec 26 2018
next sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 12/26/18 5:23 PM, Your Name wrote:
 Is there a way to declare a static array with an automatic length, so 
 that it doesn't have to be manually calculated?
 
 Perhaps something like this:
 int[auto] arr = [12, 23, 54, 653, 2, 6432, 345, 435, 26];
 
 the current way to do it is this:
 int[9] arr = [12, 23, 54, 653, 2, 6432, 345, 435, 26];
 
 But then you have to manually count the items, and update the number 
 each time the array is changed.
 
 Is there any way to declare a static array with an automatically 
 inferred length?
Just added recently: https://dlang.org/phobos/std_array.html#.staticArray i.e.: auto arr = [12, 23, 54, 653, 2, 6432, 345, 435, 26].staticArray; Hm... noticed just now the table at the top of that page doesn't have staticArray listed. -Steve
Dec 26 2018
next sibling parent reply Your Name <email example.com> writes:
On Wednesday, 26 December 2018 at 22:34:02 UTC, Steven 
Schveighoffer wrote:
 auto arr = [12, 23, 54, 653, 2, 6432, 345, 435, 26].staticArray;
How does this work?
Dec 26 2018
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 12/26/18 5:37 PM, Your Name wrote:
 On Wednesday, 26 December 2018 at 22:34:02 UTC, Steven Schveighoffer wrote:
 auto arr = [12, 23, 54, 653, 2, 6432, 345, 435, 26].staticArray;
How does this work?
It creates a static array from the given data, using the compiler's IFTI engine to infer the length. And no allocations should happen. -Steve
Dec 26 2018
prev sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 12/26/18 5:34 PM, Steven Schveighoffer wrote:
 Hm... noticed just now the table at the top of that page doesn't have 
 staticArray listed.
https://github.com/dlang/phobos/pull/6817 -Steve
Dec 26 2018
prev sibling next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 26 December 2018 at 22:23:02 UTC, Your Name wrote:
 Is there a way to declare a static array with an automatic 
 length, so that it doesn't have to be manually calculated?
Yes, there is a library function from std.array: http://dpldocs.info/experimental-docs/std.array.staticArray.1.html --- import std.array; auto a = [0, 1].staticArray; // usage here, note auto static assert(is(typeof(a) == int[2])); assert(a == [0, 1]); --- Though, if you want static allocation but not necessarily static type, you can also just use... the static keyword! static int[] a = [1, 2, 3]; That would be typed int[], of course, but it would have size 3 there and would be in a statically allocated block (which also means btw the array is evaluated at compile time, and thus subject to those rules). So depends on exactly what you need, I'm guessing that lib function is it, but you should consider the static keyword option too as it is a good pattern to know anyway.
Dec 26 2018
prev sibling parent Walter Bright <newshound2 digitalmars.com> writes:
On 12/26/2018 2:23 PM, Your Name wrote:
 Is there a way to declare a static array with an automatic length, so that it 
 doesn't have to be manually calculated?
This does not work due to the circular reference: int[arr.length] arr = [1,2,3]; This works: enum array = [1,2,3]; int[array.length] arr = array;
Dec 29 2018