Skip to content

Potentially eliminating Array.create<T> #853

@dcodeIO

Description

@dcodeIO

So I've been thinking about the new Array<T>(10) problematic a bit, and I figured that it might be worth to not throw on creating a holey array, but to instead throw when accessing a holey value. For instance, currently

class Foo {}
var arr = new Array<Foo>(10); // throws

but this could be modified to

class Foo {}
var arr = new Array<Foo>(10);
arr[0]; // throws

making

class Foo {}
var arr = new Array<Foo>(10);
for (let i = 0; i < 10; ++i) arr[i] = new Foo();
arr[0]; // work

at the expense of an additional runtime check whether a value is null on element access, if the array's value type is non-nullable. Assuming that branch prediction will do fine there since actually reading a null is unlikely and only happens in error cases. Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions