operator overloading when a.type == "number"

If I write a multiplication function for my Vector2 class, it works fine if I do myVec2 * 5 but the function never even gets called if I do 5 * myVec2 which is pretty unintuitive and annoying and error prone.

I'm not really sure what the correct solution would be, but maybe Number could at least have something like
if b.type == "object" then b+a end
since you can't overload operators for it.

I can agree with that. Ideally, it should work as you expected it to work.

The main consideration in the current implementation was that performance-wise, checking only the type of the first operand means 1 less test case for every addition or multiplication, which should save a lot of CPU. Does it? I did a quick test and skipping the test for the second argument doesn't seem to have a significant performance impact. I need to do some more testing first but I think I will indeed change it to allow writing stuff like 5 * myVec2 (this implies your implementation of Vec2."*" will be responsible for checking the types of a and b to perform the correct operation!).

Post a reply



Validate your e-mail address to participate in the community