One way is to implement constructor is this way:
this(int x, int y)
{
auto i2 = cast(int[2]*)&ints;
*i2 = [x, y];
}
const is readonly view, so the constructor creates mutable view i2
and 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 ints
mutable and private, then provide public accessor function:
struct Foo {
private int[2] _ints;
this(int x, int y) {
_ints = [x, y];
}
@property ref const(int)[2] ints () {
return _ints;
}
}
Compiler may be able to inline it.