Skip to content

Commit

Permalink
[doc] Rename ti.struct_class to ti.dataclass (#5706)
Browse files Browse the repository at this point in the history
  • Loading branch information
strongoier committed Aug 10, 2022
1 parent 441e270 commit ca5bf7d
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions docs/lang/articles/advanced/odop.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,20 +278,20 @@ print(a.num()) # 6
print(b.num()) # 7
```

## Python classes as Taichi struct types
## Taichi dataclasses

Taichi provides custom [struct types](../type/type.md#compound-types) for developers to associate pieces of data together. However, it is often convenient to have:
1. A Python representation of the struct type which is more object oriented.
2. Functions associated with a struct type. (C++ style structs)


To achieve these two points, developers can use the `@ti.struct_class` decorator on a Python class. This is heavily inspired by the Python [dataclass](https://docs.python.org/3/library/dataclasses.html) feature, which uses class fields with annotations to create data types.
To achieve these two points, developers can use the `@ti.dataclass` decorator on a Python class. This is heavily inspired by the Python [dataclass](https://docs.python.org/3/library/dataclasses.html) feature, which uses class fields with annotations to create data types.

### Creating a struct from a Python class
Here is an example of how we could create a Taichi struct type from a Python class:

```python
@ti.struct_class
@ti.dataclass
class Sphere:
center: vec3
radius: ti.f32
Expand All @@ -301,7 +301,7 @@ This will create the *exact* same type as doing:
```python
Sphere = ti.types.struct(center=vec3, radius=ti.f32)
```
Using the `@ti.struct_class` decorator will convert the annotated fields in the Python class to members in the resulting struct type. In both of the above examples you would create a field of the struct the same way.
Using the `@ti.dataclass` decorator will convert the annotated fields in the Python class to members in the resulting struct type. In both of the above examples you would create a field of the struct the same way.

```python
sphere_field = Sphere.field(shape=(n,))
Expand All @@ -311,7 +311,7 @@ sphere_field = Sphere.field(shape=(n,))
Python classes can have functions attached to them, as can Taichi struct types. Building from the above example, here is how one would add functions to the struct.

```python
@ti.struct_class
@ti.dataclass
class Sphere:
center: vec3
radius: ti.f32
Expand Down Expand Up @@ -341,9 +341,9 @@ def get_area() -> ti.f32:
get_area() # 201.062...
```

### Notes on struct classes
- Inheritance of struct classes is not implemented.
- While functions attached to a struct with the `@ti.struct_class` decorator is convenient and encouraged, it is actually possible to associate a function to structs with the older method of defining structs. As mentioned above, the two methods for defining a struct type are identical in their output. To do this, use the `__struct_methods` argument with the `ti.types.struct` call:
### Notes
- Inheritance of Taichi dataclasses is not implemented.
- While functions attached to a struct with the `@ti.dataclass` decorator is convenient and encouraged, it is actually possible to associate a function to structs with the older method of defining structs. As mentioned above, the two methods for defining a struct type are identical in their output. To do this, use the `__struct_methods` argument with the `ti.types.struct` call:

```python
@ti.func
Expand Down

0 comments on commit ca5bf7d

Please sign in to comment.