Skip to content
This repository has been archived by the owner on Nov 7, 2018. It is now read-only.

Proposal: Remove fixed-length array types and replace with a variant constructor for StructType #1

Closed
nikomatsakis opened this issue Jun 20, 2014 · 1 comment
Milestone

Comments

@nikomatsakis
Copy link
Collaborator

Currently you can create a fixed-length array type using the arrayType method, e.g., Color.arrayType(1024).arrayType(768). However, when extending to value types, this introduces an ambiguity: did you intend to have an array of value types, or a value type that is an array?

A possible solution to this dilemna is to instead extend the StructType (and ValueType) constructors so that they can take one of two forms:

new StructType({fields}, [{options}]) // as today
new StructType(elementType, count, [{options}])

The overload would be based on the type of the second parameter: if it is number, then this is an array-like constructor.

So for example a 2-D array of colors would be created like so:

var ColorType = new StructType({r: uint8, g: uint8, b: uint8, a: uint8});
var ImageType = new StructType(new StructType(ColorType, 768), 1024);

The idea is that new StructType(ColorType, 768) is basically shorthand (very short) for:

var ImageType = new StructType({0: ColorType, ..., 767: ColorType});

If we did go down this route, I'd also be inclined to rename StructType to ObjectType, so that there are two meta type descriptors: ObjectType (instances of which are typed objects) and ValueType (instances of which are typed values).

@littledan
Copy link

I like this approach. One thing I might add is to restrict fixed-size arrays of ValueType to an arbitrary maximum (64?) to avoid abuse and make == not take too long. It'd still be possible to make it very slow by making an array of arrays, but at least it wouldn't be as easy to do that completely accidentally.

@nikomatsakis nikomatsakis added this to the rev1 milestone Nov 25, 2015
nikomatsakis pushed a commit that referenced this issue Jun 8, 2018
Remove description of transparent typed objects, and ctor overload for indexed TOs
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants