Essentially, I want to be able to do something like this:
struct Foo
{
const(int)[2] ints;
this(int x, int y)
{
ints = [x, y];
}
}
but this doesn’t work. The compiler (DMD 2.048) just complains that ints isn’t mutable.
How are you supposed to initialise the array?
One way is to implement constructor is this way:
const is readonly view, so the constructor creates mutable view
i2and assign to it. I really don’t like the cast in first line, maybe there is some function in std lib that encapsulates the cast and removes const modifier from type of variable, so this can be expressed in safe and idiomatic manner.Second way is to make
intsmutable and private, then provide public accessor function:Compiler may be able to inline it.