I am using the D programming language.
I want to have a struct
containing a multidimensional static array of int
s initially filled with a non-zero value (in my case, zero is a valid entry, and I want to initially mark all entries as invalid).
As it is a struct
, it can not have a default constructor.
Instead, I can supply a default value for the member of the struct
.
The question is: how do I write this multidimensional array value in a short and readable way? Is there any convenience function, special-case syntax or idiom to do that?
Here is what I came up with.
import std.range;
import std.stdio;
struct S
{
static immutable int SIZE = 3;
static immutable int NA = -1;
int [SIZE] [SIZE] a = NA.repeat(SIZE).array().repeat(SIZE).array();
}
void main()
{
S s;
writeln(s);
}
This prints the array of -1
s as expected:
S([[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]])
Still, the expression NA.repeat(SIZE).array().repeat(SIZE).array()
looks lengthy, and I suspect there could be a better (more idiomatic, more readable) way to express my intent.
Update with a few more attempts:
int [SIZE] [SIZE] a = NA;
does not compile, even with the current beta: dmd-2.066-b2.int [SIZE] [SIZE] a = NA.repeat (SIZE).array ();
compiles and does the thing. Still, the consistency suffers.int [SIZE] [SIZE] a = [NA, NA, NA];
looks like it is essentially the above expression, simplified. It compiles but fills only the first three-element subarray withNA
s. The other two subarrays contain some garbage-like stuff. Is that a partial initialization feature of some kind? To me, it looks more like a bug, like, compiler accepting invalid code.int [SIZE] [SIZE] a = [NA];
sets the first subarray to[-1, 0, 0]
and the rest to the same garbage as the previous attempt.There is also
fill
instd.algorithm
, but it works for ranges (not ranges of ranges), and does not look like it's readily usable in an initializer. At least it won't be shorter.