Add some builtin types methods

This commit is contained in:
Shunsuke Shibayama 2023-02-03 13:34:16 +09:00
parent c97b8d61ad
commit c44355346e
4 changed files with 160 additions and 1 deletions

View file

@ -1,4 +1,5 @@
from _erg_nat import Nat
from _erg_nat import NatMut
from _erg_result import Error
class Bool(Nat):
@ -15,3 +16,27 @@ class Bool(Nat):
return "False"
def __repr__(self) -> str:
return self.__str__()
def mutate(self):
return BoolMut(self)
def invert(self):
return Bool(not self)
class BoolMut(NatMut):
value: Bool
def __init__(self, b: Bool):
self.value = b
def __repr__(self):
return self.value.__repr__()
def __eq__(self, other):
if isinstance(other, bool):
return self.value == other
else:
return self.value == other.value
def __ne__(self, other):
if isinstance(other, bool):
return self.value != other
else:
return self.value != other.value
def invert(self):
self.value = self.value.invert()

View file

@ -19,6 +19,8 @@ class Str(str):
return StrMut(self)
def to_int(self):
return Int(self) if self.isdigit() else None
def contains(self, s):
return s in self
def __add__(self, other):
return then__(str.__add__(self, other), Str)
def __radd__(self, other):