slang
slang copied to clipboard
Operator overloading inside a struct
Would it be possible to have this feature where we can define operator overloading inside the struct?
struct Quaternion {
daxa_f32vec3 v;
daxa_f32 w;
__init(daxa_f32vec3 v, daxa_f32 w) {
this.v = v;
this.w = w;
}
__init(daxa_f32 x, daxa_f32 y, daxa_f32 z, daxa_f32 w) {
this.v = daxa_f32vec3(x, y, z);
this.w = w;
}
Quaternion operator*(Quaternion q1) { // <--- this guy
return Quaternion(
daxa_f32vec3(w * q1.v.x + v.x * q1.w + v.y * q1.v.z - v.z * q1.v.y,
w * q1.v.y + v.y * q1.w + v.z * q1.v.x - v.x * q1.v.z,
w * q1.v.z + v.z * q1.w + v.x * q1.v.y - v.y * q1.v.x),
w * q1.w - v.x * q1.v.x - v.y * q1.v.y - v.z * q1.v.z
);
}
In addition to this one?
Quaternion operator*(Quaternion q, Quaternion q1) {
return Quaternion(
daxa_f32vec3(q.w * q1.v.x + q.v.x * q1.w + q.v.y * q1.v.z - q.v.z * q1.v.y,
q.w * q1.v.y + q.v.y * q1.w + q.v.z * q1.v.x - q.v.x * q1.v.z,
q.w * q1.v.z + q.v.z * q1.w + q.v.x * q1.v.y - q.v.y * q1.v.x),
q.w * q1.w - q.v.x * q1.v.x - q.v.y * q1.v.y - q.v.z * q1.v.z
);
Cheers, Jaime.