Concrete generics support
Closed this issue · 2 comments
sobolevn commented
Right now it is impossible to write a code like this one:
from classes import typeclass
from typing import List
@typeclass
def some(instance) -> int:
...
@some.instance(List[int])
def _some_list_int(instance: List[int]) -> int:
...Because, you cannot check that some variable has type List[int] in runtime.
Maybe we can use runtime typecheckers? Like: https://github.com/erezsh/runtype
Related #8
sobolevn commented
sobolevn commented
My current design:
from typing import List, TYPE_CHECKING
if not TYPE_CHECKING:
reveal_type = print
from phantom import Phantom
from phantom.predicates import collection, generic
class ListOfInt(
List[int],
Phantom,
predicate=collection.every(generic.of_type(int)),
):
...
from classes import typeclass
@typeclass
def sum_all(instance) -> int:
...
@sum_all.instance(List[int], delegate=ListOfInt)
def _sum_list_of_int(instance: List[int]) -> int:
return sum(x for x in instance)
l = [1, 2, 3]
reveal_type(sum_all(l))It both typechecks (I need to fix my plugin, though) and works in runtime.