mirror of
https://github.com/erg-lang/erg.git
synced 2025-07-07 13:15:21 +00:00
49 lines
1.2 KiB
Python
49 lines
1.2 KiB
Python
Point = Class {x = Int; y = Int}
|
|
Point.
|
|
new x, y = Point {x; y}
|
|
norm self = self::x**2 + self::y**2
|
|
Point|<: Add(Point)|.
|
|
Output = Point
|
|
# This is same as `__add__ self, other: Point = ...`
|
|
`_+_` self, other: Point =
|
|
Point.new(self::x + other::x, self::y + other::y)
|
|
Point|<: Mul(Point)|.
|
|
Output = Int
|
|
`*` self, other: Point =
|
|
self::x * other::x + self::y * other::y
|
|
Point|<: Eq|.
|
|
`==` self, other: Point =
|
|
self::x == other::x and self::y == other::y
|
|
|
|
p = Point.new 1, 2
|
|
|
|
q = Point.new 3, 4
|
|
|
|
r: Point = p + q
|
|
s: Int = p * q
|
|
assert s == 11
|
|
assert r == Point.new 4, 6
|
|
assert r.norm() == 52
|
|
|
|
MyList = Class {
|
|
.list = List(Obj)
|
|
}
|
|
MyList|<: Iterable(Obj)|.
|
|
Iter = ListIterator(Obj)
|
|
iter self = self.list.iter()
|
|
MyList|<: Sized|.
|
|
__len__ self = len self.list
|
|
MyList|<: Container(Obj)|.
|
|
__contains__ self, x: Obj = x in self.list
|
|
MyList|<: Sequence(Obj)|.
|
|
__getitem__ self, idx = self.list[idx]
|
|
|
|
MyList2 = Class {
|
|
.list = List(Obj)
|
|
}
|
|
MyList2|<: Sequence(Obj)|.
|
|
Iter = ListIterator(Obj)
|
|
iter self = self.list.iter()
|
|
__contains__ self, x: Obj = x in self.list
|
|
__len__ self = len self.list
|
|
__getitem__ self, idx = self.list[idx]
|