You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/type_annotations.md
+17-1Lines changed: 17 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -41,13 +41,29 @@ In general string type hints must be used to provide Pyccel with information abo
41
41
42
42
## Tuples
43
43
44
-
Currently Pyccel supports tuples used locally in functions and in certain cases as arguments, but not as returned objects or module variables. The implementation of the type annotations (including adding the missing support) is in progress. Currently homogeneous tuple type annotations are supported for local variables and function arguments and results (if the tuples contain scalar objects). Internally we handle homogeneous tuples as though they were NumPy arrays. When creating multiple dimensional tuples it is therefore important to ensure that all objects have compatible sizes otherwise they will be handled as inhomogeneous tuples.
44
+
Currently Pyccel supports tuples used locally in functions and in certain cases as arguments, but not as returned objects or module variables. The implementation of the type annotations (including adding the missing support) is in progress.
45
+
46
+
Tuples can be homogeneous or inhomogeneous. A homogeneous tuple is a tuple whose elements all have the same type and shape. Pyccel translates homogeneous tuples in a similar way to NumPy arrays. When creating multiple dimensional tuples it is therefore important to ensure that all objects have compatible sizes otherwise they will be handled as inhomogeneous tuples. An inhomogeneous tuple describes all other types, but comes with extra restrictions. An inhomogeneous tuple is translated to multiple objects in the target language so it can only be used if the element can be identified during the translation. This means that expressions such as `a[i]` are not possible for inhomogeneous tuples while `a[0]` is valid.
47
+
48
+
Homogeneous tuple type annotations are supported for local variables and function arguments (if the tuples contain scalar objects).
45
49
46
50
To declare a homogeneous tuple the syntax is as follows:
47
51
```python
48
52
a : tuple[int,...] = (1,2,3,4)
49
53
```
50
54
55
+
Inhomogeneous tuple type annotations are supported for local variables.
56
+
57
+
To declare an inhomogeneous tuple the syntax is as follows:
58
+
```python
59
+
a : tuple[int,bool] = (1,False)
60
+
```
61
+
62
+
It is of course possible to create an inhomogeneous tuple in place of a homogeneous tuple to benefit from code optimisations that can arise from using multiple scalars in place of an array object. This will however imply the same restrictions as any other inhomogeneous tuple. E.g:
63
+
```python
64
+
a : tuple[int, int] = (1,2)
65
+
```
66
+
51
67
## Lists
52
68
53
69
Lists are in the process of being added to Pyccel. Homogeneous lists can be declared in Pyccel using the following syntax:
0 commit comments