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

17 lines
439 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