I’d like to create a vector struct in D that works like this:
vec u, v;
vec w = [2,6,8];
v.x = 9; // Sets x
v[1] = w.y; // Sets y
u = v; // Should copy data
Later I’d also like to add stuff like u = v * u etc. But the above will do for now.
This is how far I’ve come:
struct vec3f
{
float[3] data;
alias data this;
@property
{
float x(float f) { return data[0] = f; }
float y(float f) { return data[1] = f; }
float z(float f) { return data[2] = f; }
float x() { return data[0]; }
float y() { return data[1]; }
float z() { return data[2]; }
}
void opAssign(float[3] v)
{
data[0] = v[0];
data[1] = v[1];
data[2] = v[2];
}
}
Now this pretty much makes it work like I wanted, but I feel very unsure about if this is “right”. Should the opAssign() perhaps return some value?
I’m also wondering if this is really as fast as it can be? Is I’ve tried adding alias data[0] x; etc. but that doesn’t work. Any ideas? Or is this “how it’s done”? Perhaps the compiler is smart enough to figure out the propery functions are more or less aliases?
Overall, this looks pretty reasonable. For the purpose of assignment chaining, opAssign should arguably return v. However, in practice this is often overlooked and might cause a performance hit (I don’t know). Unlike in D1, you can return static arrays from functions in D2.
As far as performance, the best way to think of this is at the assembly level. Assuming inlining is enabled,
x()will almost certainly be inlined. Static arrays are stored directly in the struct without an extra layer of indirection. The instructionreturn data[0];will cause the compiler to generate code to read from an offset from the beginning of the struct. This offset will be known at compile time. Therefore, most likely callingx()will generate exactly the same assembly instructions as ifxwere actually a public member variable.One other possibility, though, would be to use an anonymous union and struct:
Note, though that
alias thisis fairly buggy right now, and you probably shouldn’t use it yet unless you’re willing to file some bug reports.