Skip to content

[Lang] Support annotated assignment #2610

Description

@xumingkuan

Concisely describe the proposed feature
We would like to support annotated assignment in the future, for example,

a: ti.i32 = 1

Since Taichi is statically typed, code snippet like this (which compiles in Python) would not compile in Taichi:

a: int = 1
a: float = 2.0

Describe the solution you'd like (if any)
Add a method build_AnnAssign in stmt_builder.py. The content of the method will be similar to build_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:

<i32> $0 = alloca
<i32> $1 = const [1]
<i32> $2 : local store [$0 <- $1]

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.

Metadata

Metadata

Assignees

Labels

feature requestSuggest an idea on this project

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions