3 ms·
I would imagine operator overloading helps with vectors so you can do things like: public static Vector operator -(Vector a, Vector b) { Vector
by rmeby 4y ago
I would imagine operator overloading helps with vectors so you can do things like:
public static Vector operator -(Vector a, Vector b)
{
Vector v = new Vector();
v.X = a.X - b.X;
v.Y = a.Y - b.Y;
return v;
}
So then you can just write:
Vector result = myVector1 - myVector2;
- kevingadd 4y agoAlso if you wrote the JS equivalent of that, each vector would be 3-4 individually heap-allocated doubles in v8, because they aren't able to allocate floating-point values inline. There would also be a new heap allocation for the result vector itself when performing each operation. The overhead really adds up. :/ At one point Mozilla had a prototype implementation of strongly-typed value types in JavaScript, but it fell by the wayside once WebAssembly became a thing.
- ygra 4y agoYou can use an array to keep the doubles together. V8 has something like half a dozen different array types, depending on what is inside and how it's used.
- throwaway23532 4y agoWhy not implement a method? var result = myvector1.sub(myvector2)