Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

We need TypedTuple #218

Closed
open-novel opened this issue Jan 31, 2021 · 14 comments
Closed

We need TypedTuple #218

open-novel opened this issue Jan 31, 2021 · 14 comments

Comments

@open-novel
Copy link

The feature of Tuple, that comparisons between sequences of the same value are considered the same, comes in handy when dealing with binaries in JavaScript.
Therefore, we need an API like the following.
Uint8Tuple, Int8Tuple, ......., Float64Tuple

@lygstate
Copy link

Make sense

@rricard
Copy link
Member

rricard commented Feb 25, 2021

Hello, can you give more details on how you would use those structures? It could make a potential follow-on proposal but we would have to understand use cases better first.

@Jack-Works
Copy link
Member

Binary data that can be compared with ===

@rricard
Copy link
Member

rricard commented Feb 25, 2021

Ok, so to me that looks similar to #134, do I have it right? Additionally, I'd like to know how you would construct the binary data on both sides of the ===?

@ghost
Copy link

ghost commented Feb 27, 2021

The API ought to be equivalent to that of %TypedArray%:

const binary = Uint8Tuple.of(-1, -2, -3, -4); // Uint8Tuple(4) [ 255, 254, 253, 252 ]

const new_binary = binary.map(u8 => u8 >>> 1); // Uint8Tuple(4) [ 127, 127, 126, 126 ]

const u32_view = Uint32Tuple(binary.buffer); // Uint32Tuple(1) [ 4244504319 ]

u32_view[0] = 2; // TypeError: Cannot assign to read only property '0' of Uint32Tuple

Uint8Tuple.from(Uint8Array.of(1, 2, 3)); // Uint8Tuple(3) [ 1, 2, 3 ]

I believe that such a follow-on proprosal would require introduction of read-only ArrayBuffers alongside it.

@bionicles
Copy link

bionicles commented Apr 5, 2021

typed records and tuples would be huge for API contracts, codegen, serialization/deserialization, parsing environment variables. +infinity from me. Would use

a typed record is equivalent to a Struct, a convention in other languages also. We could use this to easily map between many languages

@Jack-Works
Copy link
Member

Please take a look at https://github.com/Jack-Works/proposal-freeze-arraybuffer-and-readonly-view
It might be able to integrate with the R&T.

@ghost
Copy link

ghost commented Apr 6, 2021

a typed record is equivalent to a Struct, a convention in other languages also...

I strongly disagree with this, a struct can store different types, a DataView is closer to a struct. Typed tuples would merely be immutable arrays of a statically-sized integer/float type.

Hopefully we can all agree on, and understand what we are trying to introduce here.

@bionicles
Copy link

@crimsoncodes0 totally agree on tuples

sounds like we're talking about different stuff. do you mean, a TypedRecord would be a map, where all the values were the same type? If so, I'm fine with it! I just also think Structs would be handy for javascript, where we could have a variety of different types for the values of different keys.

However, these nice ideas, are almost surely out of scope of the records and tuples proposal, and take us into the realm of runtime static types for javascript, which is a huge can of worms. I'd suggest we finish the records and tuples, and then we could build on it, to bring runtime static types into JS, and make sure both TypedTuples, TypedRecords, and Structs, wind up in that proposal

@ghost
Copy link

ghost commented Apr 11, 2021

@bionicles TypedTuples, having been brought so late, would almost certainly have to be a follow-up proposal, being standardized well after R&T has been standardized or possibly implemented.

Structs may be taken care of by the Wasm<->es typed objects proposal when it comes around.

And I was not suggesting anything about typed records or maps, merely that structs may contain other values than the same type.

A TypedTuple would only hold a single type, e.g. Uint8Tuple only holds Uint8s, whereas a C struct could be the following:

struct T {
    Uint8 x;
    Uint32 y;
};

T::x and T::y have completely different types, this couldn't be represented in a TypedTuple.

With a DataView, one could do the following:

function readFromStruct(dataViewOfT) {
    const x = dataViewOfT.getUint8(0, true);
    const y = dataViewOfT.getUint32(1, true);
}

Terribly unergonomic, but better than an array.

@bionicles
Copy link

sounds like TypedTuple could save a lot of memory!

@littledan
Copy link
Member

It's not the most ergonomic solution, but you can sort of encode a typed tuple as a BigInt. This is relatively efficient in memory and supports ===. I agree with @rricard 's labeling of this as a follow-on proposal, since between ordinary Tuple and BigInt, you can get by to start, and we have time to see what will be needed.

@acutmore
Copy link
Collaborator

acutmore commented Jul 3, 2021

Related page on the internet: https://github.com/rbuckton/proposal-struct
I'm mentioning this other proposal not as an endorsement but for the sole reason that it might interest some participants in this thread

@rricard
Copy link
Member

rricard commented Jul 8, 2022

In order to focus on changes that are necessary to finish this proposal, we are closing issues related to follow-on proposals. You are free to continue the discussion here and reference it in other venues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants