From ebac63615ca8e92a3da0f77b3571ec6d09ff68be Mon Sep 17 00:00:00 2001 From: Gabriel Vainer <67750020+vai9er@users.noreply.github.com> Date: Mon, 12 Dec 2022 04:33:52 -0500 Subject: [PATCH 1/5] Update dataclass.md --- docs/lang/articles/advanced/dataclass.md | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/docs/lang/articles/advanced/dataclass.md b/docs/lang/articles/advanced/dataclass.md index 357bbd5097891..d151aad14f943 100644 --- a/docs/lang/articles/advanced/dataclass.md +++ b/docs/lang/articles/advanced/dataclass.md @@ -6,13 +6,13 @@ sidebar_position: 4 Taichi provides custom [struct types](../type_system/type.md#compound-types) for developers to assemble pieces of data together. However, it would be more convenient to have: -- A Python representation of the struct type which is more object oriented. -- Functions associated with a struct type (C++-style structs). +- A Python representation of the struct type which is more Object-Oriented. +- Functions associated with a struct type. To achieve the ends, Taichi enabled the `@ti.dataclass` decorator on a Python class. This is inspired by Python's [dataclass](https://docs.python.org/3/library/dataclasses.html) feature, which uses class fields with annotations to create data types. -## Create a struct from a Python class +## Create a Struct from a Python Class The following is an example of defining a Taichi struct type under a Python class: @@ -22,19 +22,16 @@ class Sphere: center: vec3 radius: ti.f32 ``` -This creates the *exact* same type as using `ti.types.struct()`: +This is the same equivalent as using `ti.types.struct()`: ```python Sphere = ti.types.struct(center=vec3, radius=ti.f32) ``` -The `@ti.dataclass` decorator converts the annotated members in the Python class to members in the resulting struct type. In both of the above examples, you end up with the same struct field. +The `@ti.dataclass` decorator converts the annotated members in the `Python class` to members in the resulting `Struct Type`. In both of the above examples, you end up with the same struct field. -```python -sphere_field = Sphere.field(shape=(n,)) -``` -## Associate functions with the struct type -Python classes can have functions attached to them, and so can Taichi struct types. Building from the above example, one can embed functions in the struct as follows: +## Associate Functions with the Struct Type +Both Python classes and Taichi struct types can have functions attached to them. Building from the above example, one can embed functions in the struct as follows: ```python @ti.dataclass @@ -54,7 +51,7 @@ class Sphere: Functions associated with structs follow the same scope rules as other functions. In other words, they can be placed in either the Taichi scope or the Python scope. Each instance of the `Sphere` struct type now have the above functions attached to them. The functions can be called in the following way: -```python +```python{3,10} a_python_struct = Sphere(center=vec3(0.0), radius=1.0) # calls a python scope function from python a_python_struct.is_zero_sized() # False From 558724f2f52d57d805304aaee9e30df929ea137c Mon Sep 17 00:00:00 2001 From: Gabriel Vainer <67750020+vai9er@users.noreply.github.com> Date: Mon, 12 Dec 2022 22:39:09 -0500 Subject: [PATCH 2/5] Update docs/lang/articles/advanced/dataclass.md Co-authored-by: Zhao Liang --- docs/lang/articles/advanced/dataclass.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/lang/articles/advanced/dataclass.md b/docs/lang/articles/advanced/dataclass.md index d151aad14f943..63c9a82cc45da 100644 --- a/docs/lang/articles/advanced/dataclass.md +++ b/docs/lang/articles/advanced/dataclass.md @@ -31,6 +31,7 @@ The `@ti.dataclass` decorator converts the annotated members in the `Python clas ## Associate Functions with the Struct Type + Both Python classes and Taichi struct types can have functions attached to them. Building from the above example, one can embed functions in the struct as follows: ```python From 693b7db4e0cc86c9e1fbd2f62470474d31d3ec0c Mon Sep 17 00:00:00 2001 From: Olinaaaloompa <106292061+Olinaaaloompa@users.noreply.github.com> Date: Wed, 14 Dec 2022 10:36:16 +0800 Subject: [PATCH 3/5] Update docs/lang/articles/advanced/dataclass.md --- docs/lang/articles/advanced/dataclass.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/lang/articles/advanced/dataclass.md b/docs/lang/articles/advanced/dataclass.md index 63c9a82cc45da..faa2829143053 100644 --- a/docs/lang/articles/advanced/dataclass.md +++ b/docs/lang/articles/advanced/dataclass.md @@ -12,7 +12,7 @@ Taichi provides custom [struct types](../type_system/type.md#compound-types) for To achieve the ends, Taichi enabled the `@ti.dataclass` decorator on a Python class. This is inspired by Python's [dataclass](https://docs.python.org/3/library/dataclasses.html) feature, which uses class fields with annotations to create data types. -## Create a Struct from a Python Class +## Create a struct from a Python class The following is an example of defining a Taichi struct type under a Python class: From cbd0ebafdbdf96cc4788b8be16f38116ba2bc7e3 Mon Sep 17 00:00:00 2001 From: Olinaaaloompa <106292061+Olinaaaloompa@users.noreply.github.com> Date: Wed, 14 Dec 2022 10:36:22 +0800 Subject: [PATCH 4/5] Update docs/lang/articles/advanced/dataclass.md --- docs/lang/articles/advanced/dataclass.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/lang/articles/advanced/dataclass.md b/docs/lang/articles/advanced/dataclass.md index faa2829143053..bcf5dc46b0fe9 100644 --- a/docs/lang/articles/advanced/dataclass.md +++ b/docs/lang/articles/advanced/dataclass.md @@ -30,7 +30,7 @@ Sphere = ti.types.struct(center=vec3, radius=ti.f32) The `@ti.dataclass` decorator converts the annotated members in the `Python class` to members in the resulting `Struct Type`. In both of the above examples, you end up with the same struct field. -## Associate Functions with the Struct Type +## Associate Functions with the struct type Both Python classes and Taichi struct types can have functions attached to them. Building from the above example, one can embed functions in the struct as follows: From b23e7b113710be82e4b02cfecaf4bec99f8340aa Mon Sep 17 00:00:00 2001 From: Olinaaaloompa <106292061+Olinaaaloompa@users.noreply.github.com> Date: Wed, 14 Dec 2022 10:46:00 +0800 Subject: [PATCH 5/5] Update docs/lang/articles/advanced/dataclass.md --- docs/lang/articles/advanced/dataclass.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/lang/articles/advanced/dataclass.md b/docs/lang/articles/advanced/dataclass.md index bcf5dc46b0fe9..31bc2bb8e4ad5 100644 --- a/docs/lang/articles/advanced/dataclass.md +++ b/docs/lang/articles/advanced/dataclass.md @@ -27,7 +27,7 @@ This is the same equivalent as using `ti.types.struct()`: ```python Sphere = ti.types.struct(center=vec3, radius=ti.f32) ``` -The `@ti.dataclass` decorator converts the annotated members in the `Python class` to members in the resulting `Struct Type`. In both of the above examples, you end up with the same struct field. +The `@ti.dataclass` decorator converts the annotated members in the *Python class* to members in the resulting *struct type*. In both of the above examples, you end up with the same struct field. ## Associate Functions with the struct type