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.
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).
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.
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?
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.
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.