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?