ES4 draft: Vector

Lars Hansen lhansen at adobe.com
Sat Mar 8 08:34:40 PST 2008


> -----Original Message-----
> From: Eylon Stroh 
> Sent: 7. mars 2008 12:22
> To: Lars Hansen; es4-discuss at mozilla.org
> Subject: RE: ES4 draft: Vector
> 
> << FIXME   Need to check a detail of the type system, namely 
> whether Vector.<T> is a subtype of Vector.<U> if T is a 
> subtype of U and U is not *. >>
> 
> It shouldn't be.  Consider Bar <: Foo, Baz <: Foo:
> 
> let baz:Vector.<Baz> = new Vector.<Baz>(); 
> let foo:Vector.<Foo> = baz; // allowed if Vector.<Baz> <: Vector.<Foo>

> foo.add(new Bar()); 

That would fail with a run-time check since Bar is not <: Baz.

Note that the type annotations here do not imply any absence of run-time
checks.  For this program, the annotations make no difference and could
be left out.  (Annotations only have a run-time effect in that they
cause errors to be thrown on assigning to variables and properties if
the types don't match, and they cause some primitive values to be
converted to other primitive values.)

> let bazItem:Baz = baz.get(0); // type error!

Wouldn't get that far.

Anyhow we've decided against covariant vector types 
except that Vector.<T> <: Vector.<*> for all T.

> Other comments:
> 
> push() is missing "length++;"

It is not; writing to this[length] updates length.

> sort() is missing "return this;"

Indeed.

> I think that the first for loop in unshift() should read
>
>     for ( let i=1 ; i <= oldlimit ; i++ )
>         this[newlimit-i] = this[oldlimit-i];

I think the test is i <= numitems but otherwise you're right, it needs
to run from 1.

> also, "return newlength;" should be
>     length = newlimit;
>     return length;
> assuming that the length setter only sets the length 
> property, without setting any default values (otherwise it 
> would need to be moved up to before the for loops).

Superficially this is the same issue as for push(), length has already
been updated by the assignments in the first loop.  So the explicit
update is not needed before return.  But the first loop is not actually
correct, it needs to set the new length first otherwise the assignments
will be illegal if more than one value is unshifted in.

Thanks for the careful reading.

--lars

> 
> 
> -----Original Message-----
> From: es4-discuss-bounces at mozilla.org 
> [mailto:es4-discuss-bounces at mozilla.org] On Behalf Of Lars Hansen
> Sent: Monday, March 03, 2008 12:36 PM
> To: es4-discuss at mozilla.org
> Subject: ES4 draft: Vector
> 
> I enclose a slightly incomplete/rough draft for the Vector class.
> Please comment.
> 
> --lars
> 



More information about the Es4-discuss mailing list