mirror of
https://github.com/erg-lang/erg.git
synced 2025-08-04 02:39:20 +00:00
20 lines
508 B
Python
20 lines
508 B
Python
Norm = Trait {.norm = (self: Self) -> Nat}
|
|
|
|
Point2D = Class {x = Int; y = Int}
|
|
Point2D|<: Norm|.
|
|
norm self = self::x**2 + self::y**2
|
|
|
|
Point3D = Class {x = Int; y = Int; z = Int}
|
|
Point3D|<: Norm|.
|
|
norm self = self::x**2 + self::y**2 + self::z**2
|
|
|
|
norm|T <: Norm| x: T = x.norm()
|
|
|
|
implicit_norm x = x.norm()
|
|
|
|
p = Point2D.new {x = 3; y = 4}
|
|
print! norm(p)
|
|
assert norm(p) == 25
|
|
assert norm(Point3D.new {x = 3; y = 4; z = 5}) == 50
|
|
# assert norm(1) # this should be an error
|
|
assert implicit_norm(p) == 25
|