Raise Exception in Python when attempting to operate on incompatible types, as this will be invalid in JS

This commit is contained in:
RunasSudo 2020-12-30 02:32:36 +11:00
parent f0e8bac4b7
commit 803941a53a
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
4 changed files with 72 additions and 0 deletions

View File

@ -44,23 +44,41 @@ class Fixed:
return Rational(self.impl)
def __add__(self, other):
if not isinstance(other, Fixed):
raise ValueError('Attempt to operate on incompatible types')
return Fixed(self.impl + other.impl)
def __sub__(self, other):
if not isinstance(other, Fixed):
raise ValueError('Attempt to operate on incompatible types')
return Fixed(self.impl - other.impl)
def __mul__(self, other):
if not isinstance(other, Fixed):
raise ValueError('Attempt to operate on incompatible types')
return Fixed(self.impl * other.impl)
def __truediv__(self, other):
if not isinstance(other, Fixed):
raise ValueError('Attempt to operate on incompatible types')
return Fixed(self.impl / other.impl)
def __eq__(self, other):
if not isinstance(other, Fixed):
raise ValueError('Attempt to operate on incompatible types')
return self.impl == other.impl
def __gt__(self, other):
if not isinstance(other, Fixed):
raise ValueError('Attempt to operate on incompatible types')
return self.impl > other.impl
def __ge__(self, other):
if not isinstance(other, Fixed):
raise ValueError('Attempt to operate on incompatible types')
return self.impl >= other.impl
def __lt__(self, other):
if not isinstance(other, Fixed):
raise ValueError('Attempt to operate on incompatible types')
return self.impl < other.impl
def __le__(self, other):
if not isinstance(other, Fixed):
raise ValueError('Attempt to operate on incompatible types')
return self.impl <= other.impl
def __floor__(self):

View File

@ -37,23 +37,41 @@ class NativeInt:
return Rational(self.impl)
def __add__(self, other):
if not isinstance(other, NativeInt):
raise ValueError('Attempt to operate on incompatible types')
return NativeInt(self.impl + other.impl)
def __sub__(self, other):
if not isinstance(other, NativeInt):
raise ValueError('Attempt to operate on incompatible types')
return NativeInt(self.impl - other.impl)
def __mul__(self, other):
if not isinstance(other, NativeInt):
raise ValueError('Attempt to operate on incompatible types')
return NativeInt(self.impl * other.impl)
def __truediv__(self, other):
if not isinstance(other, NativeInt):
raise ValueError('Attempt to operate on incompatible types')
return NativeInt(self.impl / other.impl)
def __eq__(self, other):
if not isinstance(other, NativeInt):
raise ValueError('Attempt to operate on incompatible types')
return self.impl == other.impl
def __gt__(self, other):
if not isinstance(other, NativeInt):
raise ValueError('Attempt to operate on incompatible types')
return self.impl > other.impl
def __ge__(self, other):
if not isinstance(other, NativeInt):
raise ValueError('Attempt to operate on incompatible types')
return self.impl >= other.impl
def __lt__(self, other):
if not isinstance(other, NativeInt):
raise ValueError('Attempt to operate on incompatible types')
return self.impl < other.impl
def __le__(self, other):
if not isinstance(other, NativeInt):
raise ValueError('Attempt to operate on incompatible types')
return self.impl <= other.impl
def __floor__(self):

View File

@ -37,23 +37,41 @@ class Native:
return Rational(self.impl)
def __add__(self, other):
if not isinstance(other, Native):
raise ValueError('Attempt to operate on incompatible types')
return Native(self.impl + other.impl)
def __sub__(self, other):
if not isinstance(other, Native):
raise ValueError('Attempt to operate on incompatible types')
return Native(self.impl - other.impl)
def __mul__(self, other):
if not isinstance(other, Native):
raise ValueError('Attempt to operate on incompatible types')
return Native(self.impl * other.impl)
def __truediv__(self, other):
if not isinstance(other, Native):
raise ValueError('Attempt to operate on incompatible types')
return Native(self.impl / other.impl)
def __eq__(self, other):
if not isinstance(other, Native):
raise ValueError('Attempt to operate on incompatible types')
return self.impl == other.impl
def __gt__(self, other):
if not isinstance(other, Native):
raise ValueError('Attempt to operate on incompatible types')
return self.impl > other.impl
def __ge__(self, other):
if not isinstance(other, Native):
raise ValueError('Attempt to operate on incompatible types')
return self.impl >= other.impl
def __lt__(self, other):
if not isinstance(other, Native):
raise ValueError('Attempt to operate on incompatible types')
return self.impl < other.impl
def __le__(self, other):
if not isinstance(other, Native):
raise ValueError('Attempt to operate on incompatible types')
return self.impl <= other.impl
def __floor__(self):

View File

@ -46,23 +46,41 @@ class Rational:
return Num(self.impl.numerator) / Num(self.impl.denominator)
def __add__(self, other):
if not isinstance(other, Rational):
raise ValueError('Attempt to operate on incompatible types')
return Rational(self.impl + other.impl)
def __sub__(self, other):
if not isinstance(other, Rational):
raise ValueError('Attempt to operate on incompatible types')
return Rational(self.impl - other.impl)
def __mul__(self, other):
if not isinstance(other, Rational):
raise ValueError('Attempt to operate on incompatible types')
return Rational(self.impl * other.impl)
def __truediv__(self, other):
if not isinstance(other, Rational):
raise ValueError('Attempt to operate on incompatible types')
return Rational(self.impl / other.impl)
def __eq__(self, other):
if not isinstance(other, Rational):
raise ValueError('Attempt to operate on incompatible types')
return self.impl == other.impl
def __gt__(self, other):
if not isinstance(other, Rational):
raise ValueError('Attempt to operate on incompatible types')
return self.impl > other.impl
def __ge__(self, other):
if not isinstance(other, Rational):
raise ValueError('Attempt to operate on incompatible types')
return self.impl >= other.impl
def __lt__(self, other):
if not isinstance(other, Rational):
raise ValueError('Attempt to operate on incompatible types')
return self.impl < other.impl
def __le__(self, other):
if not isinstance(other, Rational):
raise ValueError('Attempt to operate on incompatible types')
return self.impl <= other.impl
def __floor__(self):