nac3-spec/toy-impl/examples/a.py

28 lines
632 B
Python
Raw Normal View History

2020-12-23 11:54:48 +08:00
I = TypeVar('I', int32, float, Vec)
2020-12-23 10:34:56 +08:00
class Vec:
v: list[int32]
def __init__(self, v: list[int32]):
self.v = v
2020-12-23 11:22:17 +08:00
def __add__(self, other: I) -> Vec:
2020-12-23 11:54:48 +08:00
if type(other) == int32:
2020-12-23 11:22:17 +08:00
return Vec([v + other for v in self.v])
2020-12-23 11:54:48 +08:00
elif type(other) == float:
return Vec([v + int32(other) for v in self.v])
2020-12-23 11:22:17 +08:00
else:
return Vec([self.v[i] + other.v[i] for i in range(len(self.v))])
2020-12-23 10:34:56 +08:00
2020-12-23 16:05:45 +08:00
T = TypeVar('T', int32, list[int32])
def add(a: int32, b: T) -> int32:
if type(b) == int32:
return a + b
else:
for x in b:
a = add(a, x)
return a