-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Lang] Support annotated assignment #2610
Comments
I would like to work on this issue. A simple approach would be cast the value based on the type annotation we provide. In case of already-created variable, we compare the type annotation with the type established in the |
@woooodyye Welcome to the Taichi community! Your proposal looks great. This issue is assigned to you :-) |
* #2610 add typed build assign * add typed support * code formated for type assigned * Auto Format * wrong tester * support typed assign with value decl * Update tests/python/test_assign.py Co-authored-by: Yi Xu <[email protected]> * Update python/taichi/lang/ast/ast_transformer.py Co-authored-by: Yi Xu <[email protected]> * fixed naming and formatting error Co-authored-by: Taichi Gardener <[email protected]> Co-authored-by: Yi Xu <[email protected]>
Can we make from typing import Generic, TypeVar
import taichi as ti
from taichi.lang.struct import StructType
T = TypeVar('T')
vec3 = ti.types.vector(3, ti.float32)
vec2 = ti.types.vector(2, ti.float32)
# make IntelliSense happy
class TypedStructType(Generic[T], StructType): ...
def ti_dataclass(cls: type[T]) -> TypedStructType[T]:
return ti.dataclass(cls)
def typing(_: TypedStructType[T], obj) -> type[T]:
return obj
@ti_dataclass
class Triangle:
V0: vec3
V1: vec3
V2: vec3
TriangleT = typing(Triangle, ti.template())
@ti_dataclass
class TextureTriangle:
V0: vec2
V1: vec2
V2: vec2
TextureTriangleT = typing(TextureTriangle, ti.template())
@ti_dataclass
class Primitive:
position_triangle: Triangle
texture_triangle: TextureTriangle
material_index: ti.i32
PrimitiveT = typing(Primitive, ti.template())
@ti.func
def foo(p: PrimitiveT): # taichi allows ti.template() as argument annotation
pos = p.position_triangle
pos.V0
pos2: TriangleT = p.position_triangle # taichi doesn't allow ti.template() as assignment annotation
pos2.V0 # intelliSense enabled |
Concisely describe the proposed feature
We would like to support annotated assignment in the future, for example,
Since Taichi is statically typed, code snippet like this (which compiles in Python) would not compile in Taichi:
Describe the solution you'd like (if any)
Add a method
build_AnnAssign
instmt_builder.py
. The content of the method will be similar tobuild_Assign
. It should also set the type when creating the variable, and check if the type is the same when an already-created variable is annotated assigned again.The statement
a: ti.i32 = 1
should be translated into the following CHI IR:Additional comments
The statement corresponds to
ast.AnnAssign
in the Python AST. See https://docs.python.org/3/library/ast.html#ast.AnnAssign for more information.The text was updated successfully, but these errors were encountered: