feat: impl Hash for Array

This commit is contained in:
Shunsuke Shibayama 2023-09-18 19:44:37 +09:00
parent 53a43962e8
commit 57d3a23aed
7 changed files with 71 additions and 19 deletions

View file

@ -51,6 +51,9 @@ class Array(list):
else:
return list.__getitem__(self, index_or_slice)
def __hash__(self):
return hash(tuple(self))
def type_check(self, t: type) -> bool:
if isinstance(t, list):
if len(t) < len(self):
@ -59,6 +62,8 @@ class Array(list):
if not contains_operator(inner_t, elem):
return False
return True
elif isinstance(t, set):
return self in t
elif not hasattr(t, "__args__"):
return isinstance(self, t)
elem_t = t.__args__[0]

View file

@ -5,28 +5,16 @@ from _erg_str import Str
def int__(i):
try:
return Int(i)
except:
return None
return Int(i)
def nat__(i):
try:
return Nat(i)
except:
return None
return Nat(i)
def float__(f):
try:
return Float(f)
except:
return None
return Float(f)
def str__(s):
try:
return Str(s)
except:
return None
return Str(s)

View file

@ -3,13 +3,16 @@ from _erg_int import Int
from _erg_int import IntMut # don't unify with the above line
from _erg_control import then__
class Nat(Int):
def __init__(self, i):
if int(i) < 0:
raise ValueError("Nat can't be negative: {}".format(i))
def try_new(i): # -> Result[Nat]
if i >= 0:
return Nat(i)
else:
return Error("Nat can't be negative")
return Error("Nat can't be negative: {}".format(i))
def times(self, f):
for _ in range(self):
@ -38,6 +41,8 @@ class NatMut(IntMut): # and Nat
value: Nat
def __init__(self, n: Nat):
if int(n) < 0:
raise ValueError("Nat can't be negative: {}".format(n))
self.value = n
def __int__(self):