the "type" in Typed Arrays refers to the bit size in memory; being able to pass an arbitrary value seems like it would require implementations to calculate the precise (not just the maximum) memory size a single item requires.
On Fri, Feb 7, 2020 at 6:33 AM Andrea Giammarchi < [email protected]> wrote: > As more and more often devs are shifting into TS, or would like to have > dynamic typed arrays without a fixed length, and as v8 and likely other > engines too optimize a lot for same-kind arrays, I wonder what people here > think about this `TypedArray` rough proposal: > > # Signature: > > ```js > new TypedArray( > type, // either a "type" to infer, or a shape, or a Class > length // optional: if defined it's fixed size > ); > ``` > > > ## About the `type` > > * if the inferred type is `object`: > * if the object is `null`, throw new InvalidType > * if the object is a literal, or its `__proto__` is either `null` or > `Object.prototype, enforce own properties and respective types > * if the object is not literal, fallback to object Class (`/./` is > `RegExp`, `() => {}` is `Function`, etc) > * if the inferred `type` is `function`, fallback to Class > * if the inferred `type` is `Class`, ensure each entry is an instance of > such class, or throw InvalidType > * in every other case, make a _typed_ collection of `number`, `string`, > `boolean`, or `symbol` > > > ## About the `length` > > * if provided, the collection has a fixed, immutable, length, and each > initial entry a default value > > > # Use Cases: > > ```js > const numbers = new TypedArray(0, 5); > // creates [0, 0, 0, 0, 0] > // numbers length is immutable > // numbers accepts only typeof number > // out of bound entries throw > > let numbers = new TypedArray(0); > // numbers length is dynamic > // numbers accepts only typeof number > // out of bound entries throw > > const shapes = new TypedArray({name: '', age: 0}, 3); > // creates [{name: '', age: 0}, {name: '', age: 0}, {name: '', age: 0}] > // shapes length is immutable > // shapes accepts only objects with `name` and `age` and respective types > // out of bound entries throw > > let shapes = new TypedArray({lat: 0, long: 0}); > // shapes length is dynamic > // shapes accepts only objects with `lat` and `long` and respective types > // out of bound entries throw > > const classes = new TypedArray(HTMLElement, 2); > // creates [Object.create(HTMLElement.prototype), > Object.create(HTMLElement.prototype)] > // classes length is immutable > // classes accepts only instances of HTMLElement > // out of bound entries throw > > let classes = new TypedArray(HTMLElement); > // classes length is dynamic > // classes accepts only instances of HTMLElement > // out of bound entries throw > > // more options > let strings = new TypedArray(''); > let booleans = new TypedArray(true); > let functions = new TypedArray(Function); > let coords = new TypedArray({lat: 0, long: 0}); > ``` > > Thanks in advance for eventual thoughts on this 👋 > _______________________________________________ > es-discuss mailing list > [email protected] > https://mail.mozilla.org/listinfo/es-discuss >
_______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

