erg/tests/should_ok/structural.er
2024-08-09 13:38:04 +09:00

31 lines
687 B
Python

add|T: Type, U: Type, V: Type| x: Structural({ .__add__ = (self: T, other: U) -> V }), other: U =
x.__add__(other)
a = add 1, 2
b = add a, 2
c = add b, 2
assert c == 7
_add x, y = add x, y
x = _add 1, 2
y = _add x, 2
z = _add y, 2
assert z == 7
w = _add -1, 2
assert w == 1
gt|T, U| x: Structural({.__gt__ = (self: T, other: U) -> Bool}), y: U = x.__gt__ y
assert gt(2, True)
length|T| x: Structural { .__len__ = (self: T) -> Nat } = x.__len__()
assert length("aaa") == 3
assert length(bytes("aaa", "utf-8")) == 3
assert length([1, 2]) == 2
assert length({"a": 1}) == 1
assert length({1, 2, 3}) == 3
assert length((1, 2, 3)) == 3
s as Str or Bytes = "a"
assert length(s) == 1