mirror of
https://github.com/erg-lang/erg.git
synced 2025-08-04 02:39:20 +00:00
fix: dynamic type checking bugs
This commit is contained in:
parent
a6b72ea636
commit
5affa5065f
12 changed files with 91 additions and 7 deletions
|
@ -2,6 +2,7 @@ from _erg_control import then__
|
|||
from _erg_range import Range
|
||||
from _erg_nat import NatMut
|
||||
from _erg_int import IntMut
|
||||
from _erg_contains_operator import contains_operator
|
||||
|
||||
class Array(list):
|
||||
def dedup(self, same_bucket=None):
|
||||
|
@ -43,3 +44,22 @@ class Array(list):
|
|||
return Array(list.__getitem__(self, index_or_slice.into_slice()))
|
||||
else:
|
||||
return list.__getitem__(self, index_or_slice)
|
||||
|
||||
def type_check(self, t: type) -> bool:
|
||||
if isinstance(t, list):
|
||||
if len(t) != len(self):
|
||||
return False
|
||||
for (inner_t, elem) in zip(t, self):
|
||||
if not contains_operator(inner_t, elem):
|
||||
return False
|
||||
return True
|
||||
elif not hasattr(t, "__args__"):
|
||||
return isinstance(self, t)
|
||||
elem_t = t.__args__[0]
|
||||
l = None if len(t.__args__) != 2 else t.__args__[1]
|
||||
if l is not None and l != len(self):
|
||||
return False
|
||||
for elem in self:
|
||||
if not contains_operator(elem_t, elem):
|
||||
return False
|
||||
return True
|
||||
|
|
|
@ -5,8 +5,10 @@ from collections import namedtuple
|
|||
|
||||
# (elem in y) == contains_operator(y, elem)
|
||||
def contains_operator(y, elem):
|
||||
if hasattr(elem, "type_check"):
|
||||
return elem.type_check(y)
|
||||
# 1 in Int
|
||||
if type(y) == type:
|
||||
elif type(y) == type:
|
||||
if isinstance(elem, y):
|
||||
return True
|
||||
elif hasattr(y, "try_new") and is_ok(y.try_new(elem)):
|
||||
|
|
|
@ -7,9 +7,9 @@ class Float(float):
|
|||
|
||||
def try_new(i): # -> Result[Nat]
|
||||
if isinstance(i, float):
|
||||
Float(i)
|
||||
return Float(i)
|
||||
else:
|
||||
Error("not a float")
|
||||
return Error("not a float")
|
||||
|
||||
def mutate(self):
|
||||
return FloatMut(self)
|
||||
|
|
|
@ -5,9 +5,9 @@ from _erg_control import then__
|
|||
class Int(int):
|
||||
def try_new(i): # -> Result[Nat]
|
||||
if isinstance(i, int):
|
||||
Int(i)
|
||||
return Int(i)
|
||||
else:
|
||||
Error("not an integer")
|
||||
return Error("not an integer")
|
||||
|
||||
def succ(self):
|
||||
return Int(self + 1)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue