pull/16/head
pca006132 2021-06-09 09:45:13 +08:00 committed by pca006132
parent a642e37b9a
commit 1e80d8d285
1 changed files with 10 additions and 3 deletions

View File

@ -140,11 +140,18 @@ class Foo(EnvExperiment):
* Type variables are invariant, same as the default in Python. We disallow * Type variables are invariant, same as the default in Python. We disallow
covariant or contravariant. The compiler should mark as error if it encounters covariant or contravariant. The compiler should mark as error if it encounters
a type variable used in kernel that is declared covariant or contravariant. a type variable used in kernel that is declared covariant or contravariant.
* Code region protected by a type check, such as `if type(x) == int:`, would * A custom function `is_type(x, T)` would be provided to check whether `x` is an
instance of `T`, other methods like `type(x) == int` or `isinstance(x, int)`
would not compile. The function would be able to check generic types for
`list` and `tuple`. When running on the host, user can specify whether to use
a debug mode checking (recursively check all elements, which would be slower
for large lists) or performance mode which only check the first element of
each list. (#15)
* Code region protected by a type check, such as `if is_type(x, int):`, would
treat `x` as `int`, similar to how [typescript type guard](https://www.typescripttutorial.net/typescript-tutorial/typescript-type-guards/) works. treat `x` as `int`, similar to how [typescript type guard](https://www.typescripttutorial.net/typescript-tutorial/typescript-type-guards/) works.
```python ```python
def add1(x: Union[int, bool]) -> int: def add1(x: Union[int, bool]) -> int:
if type(x) == int: if is_type(x, int):
# x is int # x is int
return x + 1 return x + 1
else: else:
@ -152,7 +159,7 @@ class Foo(EnvExperiment):
return 2 if x else 1 return 2 if x else 1
``` ```
* Generics are instantiated at compile time, all the type checks like * Generics are instantiated at compile time, all the type checks like
`type(x) == int` would be evaluated as constants. Type checks are not allowed `is_type(x, int)` would be evaluated as constants. Type checks are not allowed
in area outside generics. in area outside generics.
* Type variable cannot occur alone in the result type, i.e. must be bound to the * Type variable cannot occur alone in the result type, i.e. must be bound to the
input parameters. input parameters.