This is follow-on proposal devired from issue#218 in Records & Tuples proposal repository.
Proposal author is Evgeniy Istomin (@MadProbe).
This proposal is stage 0 in the process document.
This proposal adds new immutable primitive data structures - typed tuples.
They are similar to existing typed arrays and tuples by concept:
- There are 11 types of typed tuples.
- Same as tuples, typed tuples are immutable and primitive.
- Have same syntax as tuples have.
- Prototypes of object representations of typed tuples share a prototype - %TypedTuple%
- %TypedTuple% has same methods as in Typle.prototype and some recent proposals like relative indexing method and array find from last
Syntax of typed tuples extends syntax of tuples by adding tokens '<', %TypedTupleTypeNameOrTypeAlias% and '>' between tokens '#' and '['
var uint32_tuple = #<uint32_t>[0, 1337, 777, 322];
uint32_tuple[0]; // 0
uint32_tuple[3]; // 322
var float64_tuple = #<double>[666, ...uint32_tuple, 42]; // tuples of other types can be "rested" to tuple of another type
function* gen() {
yield* [0, 1, 2, 3, 4];
}
console.log(#<uint8_t>[...gen()]); // Logs #<uint8_t>[0, 1, 2, 3, 4], any iterator can be used when "resting" into typed tuple
uint32_tuple[0] = 1; // TypeError: Cannot assign to read only property '0' of object '[object Uint32Tuple]'
Like in tuples, holes are prevented in syntax.
#<int>[,]; // SyntaxError, holes are disallowed by syntax
assert(#<int>[0, 1] === #<int>[0, 1]);
assert(#<int>[0, 1] !== #<uint>[0, 1]); // Typed tuples of different types are not equal even if they have same contents
assert(Object(#<int>[0, 1]) !== Object(#<int>[0, 1])); // Object wrappers are different even if they wrap same typed tuple
Constructor Name | typeof tuple | Syntax | Aliases |
---|---|---|---|
Int8Tuple | "int8-tuple" | #<int8_t>[...values] |
char, int8, signed_char |
Uint8Tuple | "uint8-tuple" | #<uint8_t>[...values] |
byte, uint8, unsigned_char |
Uint8ClampedTuple | "uint8-tuple-clamped" | #<uint8_clamped_t>[...values] |
byte_clamped, uint8_clamped, unsigned_char_clamped |
Int16Tuple | "int16-tuple" | #<int16_t>[...values] |
short, int16, char16, char16_t |
Uint16Tuple | "uint16-tuple" | #<uint16_t>[...values] |
ushort, uint16, uchar16, wchar_t, uchar16_t |
Int32Tuple | "int32-tuple" | #<int32_t>[...values] |
int, int32, char32, char32_t |
Uint32Tuple | "uint32-tuple" | #<uint32_t>[...values] |
uint, uint32, uchar32, uchar32_t, codepoint_t |
Float32Tuple | "float32-tuple" | #<float32_t>[...values] |
float, float32 |
Float64Tuple | "float64-tuple" | #<float64_t>[...values] |
double, float64 |
BigInt64Tuple | "bigint64-tuple" | #<bigint64_t>[...values] |
int64, bigint64, int64_t |
BigUint64Tuple | "biguint64-tuple" | #<biguint64_t>[...values] |
uint64, biguint64, uint64_t |