Yet another attempt at typed JS data

Andrea Giammarchi andrea.giammarchi at gmail.com
Fri Feb 7 21:13:52 UTC 2020


Any name would do, I'm rather interested in the proposal itself, and its
ergonomics/logic ;-)

On Fri, Feb 7, 2020 at 10:11 PM Scott Rudiger <scottrudiger at gmail.com>
wrote:

> `StructuredArray` or `StructArray` for short? Just throwing it out there.
>
> On Fri, Feb 7, 2020, 1:08 PM Andrea Giammarchi <
> andrea.giammarchi at gmail.com> wrote:
>
>> well, this proposal has nothing to do with Typed Arrays, as these are all
>> fixed size indeed ... scratch the TypedArray name, and use ArrayKind
>> instead, any better outcome?
>>
>> On Fri, Feb 7, 2020 at 7:51 PM Jordan Harband <ljharb at gmail.com> wrote:
>>
>>> 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 <
>>> andrea.giammarchi at gmail.com> 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
>>>> es-discuss at mozilla.org
>>>> https://mail.mozilla.org/listinfo/es-discuss
>>>>
>>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20200207/8400a29e/attachment-0001.html>


More information about the es-discuss mailing list