erg/examples/impl.er
Shunsuke Shibayama dd36942256 feat: implement some typevar-related syntax sugar
* add `TypeBoundSpec::Omitted`
* add `TypeAppArgsKind`
2023-03-01 23:35:19 +09:00

25 lines
604 B
Python

Point = Class {x = Int; y = Int}
Point.
new x, y = Point::__new__ {x; y}
norm self = self::x**2 + self::y**2
Point|<: Add(Point)|.
Output = Point
__add__ self, other: Point =
Point.new(self::x + other::x, self::y + other::y)
Point|<: Mul(Point)|.
Output = Int
__mul__ self, other: Point =
self::x * other::x + self::y * other::y
Point|<: Eq|.
__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