Meta-Functions for math operators

Propose new features and changes
MysticDude
Posts: 15
Joined: 02 Jan 2020, 05:32

Meta-Functions for math operators

27 Feb 2024, 20:18

I was working on a Vec2 lib, it would be so much nicer if we had meta-functions for math.

Example:

Code: Select all

a := Vec2(150, 80)
b := Vec2(30, 490)

c := a + b      ; c := Vec2(a.x + b.x, a.y + b.y)

d := a + 5      ; d := Vec2(a.x + 5, a.y + 5)

e := 5 - b      ; e := Vec2(5 - b.x, 5 - b.y)

class Vec2 {
    __New(x := 0, y := 0) {
        This.x := x
        This.y := y
    }

    __Add(a, b) {
        if IsNumber(a)
            return Vec2(a + b.x, a + b.y)
        if IsNumber(b)
            return Vec2(a.x + b, a.y + b)
        if !(a is Vec2)
            throw ValueError('Math error', -1, a)
        if !(b is Vec2)
            throw ValueError('Math error', -1, b)
        return Vec2(a.x + b.x, a.y + b.y)
    }

    __Sub(a, b) {
        if IsNumber(a)
            return Vec2(a - b.x, a - b.y)
        if IsNumber(b)
            return Vec2(a.x - b, a.y - b)
        if !(a is Vec2)
            throw ValueError('Math error', -1, a)
        if !(b is Vec2)
            throw ValueError('Math error', -1, b)
        return Vec2(a.x - b.x, a.y - b.y)
    }
}
I think LUA sets a good example of how it's done:

https://www.lua.org/pil/13.1.html
http://phrogz.net/lua/LearningLua_MetatableEvents.html
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Meta-Functions for math operators

29 Feb 2024, 23:50

This is more typically called "operator overloading".

lexikos wrote:
05 Dec 2022, 02:16
Operator overloading has been on my TODO list for over 10 years. I think the expression evaluator needs to be reworked before it will be feasible.
MysticDude
Posts: 15
Joined: 02 Jan 2020, 05:32

Re: Meta-Functions for math operators

01 Mar 2024, 09:10

Ah nice, glad it's already on your radar :)

In KuroilLight's example the method only accepts a single param (the other operand) - presumably there would be a way to determine if the object being invoked (this) was on the left or right? Or is it more typical for only the first operand to have its method invoked?

The wiki page for operator overloading actually notes the fact that Lua allows both.
Lua allows operator overloading as syntactic sugar for method calls with the added feature that if the first operand doesn't define that operator, the method for the second operand will be used.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Meta-Functions for math operators

02 Mar 2024, 20:51

I have used operator overloading only in strongly typed languages such as C# or C++, and have never had to consider the order of the operands in practice. I have read how Lua handles it, but don't know if it provides an answer to your question, and haven't given it much thought.
MysticDude
Posts: 15
Joined: 02 Jan 2020, 05:32

Re: Meta-Functions for math operators

03 Mar 2024, 09:46

With two params the object itself could appear as either one of them, depending on how it was invoked. In the scenario with vectors, it allows any operation between vectors and numbers. Like 5 - v = (5 - v.x, 5 - v.y) or v > 5 which I would treat as 'is the vectors magnitude greater than 5?'

The downside is both params always have to be validated, whereas with a single param, 'this' is obviously already known amd I guess the other operand must be the same type anyway? There's probably a noticeable performance impact in the former.

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 62 guests