aboutsummaryrefslogtreecommitdiff
path: root/test/metamethods.tm
blob: 6a870ee99a7a8134c8b153403ffaeda61059c0da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
struct Vec2(x,y:Int):
    func __add(a,b:Vec2; inline)->Vec2:
        return Vec2(a.x+b.x, a.y+b.y)

    func __subtract(a,b:Vec2; inline)->Vec2:
        return Vec2(a.x-b.x, a.y-b.y)

    func __multiply(a,b:Vec2; inline)->Int:
        return a.x*b.x + a.y*b.y

    func __multiply2(a:Vec2,b:Int; inline)->Vec2:
        return Vec2(a.x*b, a.y*b)

    func __multiply3(a:Int,b:Vec2; inline)->Vec2:
        return Vec2(a*b.x, a*b.y)

    func __multiply4(a,b:Vec2; inline)->Vec2:
        return Vec2(a.x*b.x, a.y*b.y)

    func __negative(v:Vec2; inline)->Vec2:
        return Vec2(-v.x, -v.y)

    func __length(v:Vec2; inline)->Int:
        return 2

func main():
    >> x := Vec2(10, 20)
    >> y := Vec2(100, 200)
    >> x + y
    = Vec2(x=110, y=220)
    >> x - y
    = Vec2(x=-90, y=-180)
    >> x * y
    = 5000
    >> x * -1
    = Vec2(x=-10, y=-20)
    >> -10 * x
    = Vec2(x=-100, y=-200)

    >> x = Vec2(1, 2)
    >> x += Vec2(10, 20)
    = Vec2(x=11, y=22)
    >> x *= Vec2(10, -1)
    = Vec2(x=110, y=-22)

    >> x = Vec2(1, 2)
    >> -x
    = Vec2(x=-1, y=-2)
    >> #x
    = 2