6 ms·
Completely unhinged idea, but I have always found the traditional way of doing operator overloading (that is as special methods of a class, like Python, C++ etc
by qsort 3y ago
Completely unhinged idea, but I have always found the traditional way of doing operator overloading (that is as special methods of a class, like Python, C++ etc.) to be a bit strange.
It would be more natural to declare the algebraic structure of one or more related classes.
For example, this is something you would do to override addition:
class C:
...
def __add__(self, other: C):
...
The way that would feel more natural to me is something like:
class C:
...
algebraic_structure Monoid<C>:
def +(fst: C, snd: C) -> C:
...
Which would also work for more complicated structure, for example:
class Datetime:
...
class Timedelta:
...
algebraic_structure AffineSpace<Datetime, Timedelta>:
def point_minus(fst: Datetime, snd: Datetime) -> Timedelta:
...
def vector_add(fst: Timedelta, snd: Timedelta) -> Timedelta:
...
def vector_inverse(vec: Timedelta) -> Timedelta:
...
Where you could automatically "deduce" unspecified methods.