Tuple()
import { Tuple } from "@fed1/jsos";
The JSFunction Tuple() creates a TTuple. Tuples allow array-like access
to their items by index. However, there are some crucial differences between
tuples, vectors and arrays:
- Tuples are always immutable, there is no mutable equivalent
- Tuples are associative, not sequential (JS objects, not arrays)
- Tuples are automatically typed using their contained items in order, meaning
- Tuples must have two or more items
a
Tuple("foo", "bar", 12)is of typeTTuple{JSString, JSString, JSNumber}notTTuple{Union{JSString, JSNumber}} - TTuples are covariant, meaning a
TTuple{JSString, JSNumber}is also aTTuple{TString, TNumber}and aTTuple{TAny, TAny}
Usage
Tuple(...items)
Parameters
| # | Parameter | Required? | Type | Default |
|---|---|---|---|---|
| ... | items | yes (2+) | TAny |
...items
The items to be added to the tuple. Their types are determined using
typeOf and the resulting tuple will be a TTuple
parameterized using these types.
Return value
A TTuple that contains the items.
Examples
Creating a tuple:
const point = Tuple(12, 18);
Tuples allow array-like access:
> point[0]
12
> point[1]
18
> point[2]
undefined
However, tuples are frozen JS objects. Setting indexes that are out of range or mutating existing indexes will throw an error in strict mode code.
> point[1] = 20
Uncaught TypeError: Cannot modify immutable tuple
Index: 1
Item type: JSNumber
Tuple type: TTuple{JSNumber,JSNumber}
Tuple definition: REPL2:1:11
Location: REPL3:1:6
Tuples are iterable:
> for (const item of point) { console.log("item:", item); }
item: 12
item: 18
undefined
Version History
| Feature | Since version |
|---|---|
| Tuple() | 1.0 |
