Operator overloading

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
KuroiLight
Posts: 327
Joined: 12 Apr 2015, 20:24
Contact:

Operator overloading

20 Oct 2017, 18:42

Operator Overloading is it supported? I didn't see anything on the Objects page. I'm trying to do something like the following:

Code: Select all

Class Point {
    __New(x, y) {
        this.x := x, this.y := y
    }

    ==(p) {
        return (p.x == this.x and p.y == this.y)
    }

    -(p) {
        return new Point(this.x - p.x, this.y - p.y)
    }
}

P1 := new Point(150, 150)
P2 := new Point(200, 100)

P3 := P1 - P2
MsgBox, % P3.x . " " . P3.y
Windows 10, Ryzen 1600, 16GB G.Skill DDR4, 8GB RX 480 | [MyScripts][MySublimeSettings] [Unlicense][MIT License]
01/24/18
[/color]
teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Operator overloading

20 Oct 2017, 20:37

Anything like you mentioned is not supported.

Code: Select all

P3 := P1 - P2
You can't subtract one object from another. But you could achieve what you want by another way:

Code: Select all

Class Point {
    __New(x, y) {
        this.x := x, this.y := y
    }

    Compare(p)  {
        return (p.x == this.x and p.y == this.y)
    }

    Decrement(p) {
        return new Point(this.x - p.x, this.y - p.y)
    }
}

P1 := new Point(150, 150)
P2 := new Point(200, 100)

MsgBox, % P1.Compare(P2)

P3 := P1.Decrement(P2)
MsgBox, % P3.x . " " . P3.y
User avatar
KuroiLight
Posts: 327
Joined: 12 Apr 2015, 20:24
Contact:

Re: Operator overloading

20 Oct 2017, 22:32

Yea, that's what I ended up doing.
I was mostly curious if there was a some special way of doing this that may have been somewhere else in the docs (that i somehow missed), I've encountered strange ways of operator overloading in other languages in the past.
Windows 10, Ryzen 1600, 16GB G.Skill DDR4, 8GB RX 480 | [MyScripts][MySublimeSettings] [Unlicense][MIT License]
01/24/18
[/color]

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada and 306 guests