-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For types such as tuples, Option, Result, and various others, there's no need to allocate them on the heap. In fact, for frequently used types such as Option and Result this does nothing but harm performance. To resolve this, various types are marked as `inline` such that they are allocated on the stack. Changelog: performance
- Loading branch information
1 parent
873adca
commit fe867a6
Showing
7 changed files
with
91 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
--- | ||
{ | ||
"title": "Tuples" | ||
} | ||
--- | ||
|
||
A tuple is a finite collection of values, possibly of different types. Tuples | ||
are created using the syntax `(A, B, ...)` where `A`, `B` and `...` represent | ||
the values stored in a tuple. For example: | ||
|
||
```inko | ||
(10, 20, 30) | ||
``` | ||
|
||
This expression returns a tuple with three values: `10`, `20` and `30`. Unlike | ||
arrays, the values don't need to be of the same type: | ||
|
||
```inko | ||
(10, 'hello', true) | ||
``` | ||
|
||
Tuples are _finite_ meaning that they have a fixed length, unlike arrays which | ||
can grow in size. This means there's no equivalent of `Array.push`, `Array.pop` | ||
and so on. | ||
|
||
## Accessing tuple values | ||
|
||
Tuple values are accessed using their position, using the syntax `the_tuple.N` | ||
where `N` is the index starting at `0`: | ||
|
||
```inko | ||
let pair = (10, 'hello') | ||
pair.0 # => 10 | ||
pair.1 # => 'hello' | ||
``` | ||
|
||
## Tuples are inline types | ||
|
||
Tuples are `inline` types and thus are stored on the stack. This means it's not | ||
possible to assign a new value to a tuple: | ||
|
||
```inko | ||
let pair = (10, 'hello') | ||
pair.0 = 20 # => not valid, resulting in a compile-time error | ||
``` | ||
|
||
## Limitations | ||
|
||
The tuple syntax is syntax sugar for creating instances of the various tuple | ||
types provided by the `std.tuple` module. For example, the expression | ||
`(10, 'hello')` is syntax sugar for creating an instance of `std.tuple.Tuple2`. | ||
The `std.tuple` module only provides types for tuples with up to 8 values, and | ||
thus tuples can only store at most 8 values. | ||
|
||
::: tip | ||
It's highly recommended to avoid creating tuples with more than three values. | ||
Instead, use a custom type when you need to store more than three values. | ||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters