Support references to array indices.

Working with typed arrays is clunky, because we cannot reference values in arrays directly. It’d be handy if we could do something like this:

const bytes = new UintArray(256);
const x = 0 of bytes;

console.log(x);         // console.log(bytes[0]);
x = 1;                  // bytes[0] = 1;

The of operator would expect an index expression on the left and an array expression on the right.

This could be generalized to any array or sequence etc, but it's principally about working with typed data more naturally, so could be limited to typed arrays.

I'm confused, you already can both get and set bytes[0].

Yeah, but you have to reference the array and index (bytes[index]), which gets messy in practice.

Typed arrays provide a indirect solution to typed values in JavaScript. Support for indirect references would make the indirection automatic (so x can act more like a reference to typed value).

…I’m also confused. Your example code in the first post also references the array and index.

It looks like your actual request is completely unconnected to TypedArray, and is actually about the ability to take references into data structures?

You only reference the array and index once (const x = index of array). Thereafter, you can use x, instead of having to write array[index] over and over again. This makes sense if you work with typed arrays a lot.

You could generalize it to allow references to values within any structure, but that just feels a bit wacky. This is much more natural in the context of typed arrays, where we're often only using an array to store typed values. The values are not arrayed in any meaningful sense. They're just stored in an array to allow for typed numbers (and optimized math).

If reference indirection was supported on regular arrays etc, I would maybe use that feature once in a while, but I could easily live without it. It is much more convenient for working with typed arrays.

Also, if reference indirection was generally supported for any structure, beginner programmers would need to deal with it fairly early on. If it's limited to typed arrays, you only need to know about it when you work with typed arrays, and it makes a lot more sense in that context.

I'm not strongly against generalizing it to all structures. I just think it's debatable.

For example, you could do stuff like this:

const i = 0 of new Uint8Array(1);

And then use i to reference a u8, without needing a reference to the array that only exists to store the u8.

If reference indirection was supported on regular arrays etc, I would maybe use that feature once in a while, but I could easily live without it. It is much more convenient for working with typed arrays.

I don’t understand why this would be the case. Arrays and Typed Arrays (and objects) are exactly identical in this regard. You use arr[index] for arrays, arr[index] for typed arrays, and obj[key] for objects.

The only thing I can come up with is that you’re using TypedArrays not as arrays at all, but just as a way to force a value to be a particular numeric type?

1 Like

The only thing I can come up with is that you’re using TypedArrays not as arrays at all, but just as a way to force a value to be a particular numeric type?

@tabatkins - Exactly. That's one of the usecases for typed arrays. Expressions on values in typed arrays of the same type are (highly) optimized.

For example, if uints was a Uint32Array, then uints[0] = uints[1] + uints[2]) would use unsigned 32-bit operands and result (without converting to and from Number). It'd be nice to be able to write x = y + z instead.

Not that I would ever do this but

const bytes = new Uint8Array(256);
const byte99 = Reflect.get.bind(Reflect.get,bytes,99);

bytes[99] = 1;
console.log(byte99()); // 1
    
bytes[99] = 2;
console.log(byte99()); // 2

If we expand on the syntactic sugar, it would probably look something like this:

const bytes = new UintArray(256); 
// const x = 0 of bytes; 
const x = Object.freeze({
    get value() { return bytes[0]; },
    set value(_) { bytes[0] = _; }
});

// console.log(x); 
console.log(x.value);         
// x = 1;
x.value = 1;                  

It seems to be something similar to proposal refs?

rbuckton/proposal-refs: Ref declarations and expressions for ECMAScript

3 Likes

People will do anything but learn C++

You write webapps in C++ ??

I like Swift, but not in the browser.

You're right. I didn't know about proposal refs, but that would cover this usecase nicely. Still, something as general as refs might be a bridge too far.

1 Like

we have wasm in the big 25

Good luck writing an actually performant web application in wasm; have you?

Please keep posts on topic.

2 Likes