Array and Object Literal Types

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Array and Object Literal Types

Garrett Smith
It would be nice to have Array literal syntax declare it's ArrayType
before the declaration of the array.

For example:

1. var x : Array<String> = [ "foo", "bar", document.title, getAnotherString() ];

2. var x = [ "foo", "bar", document.title, getAnotherString() ] : String;

example 1 is more clear to read because in a long array, you'd see
right away what type of Array it is.

Is the former syntax (or some variant thereof) going to be available?

What about for Objects?

Garrett
_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Array and Object Literal Types

Brendan Eich-2
On Aug 17, 2007, at 2:47 PM, Garrett Smith wrote:

> It would be nice to have Array literal syntax declare it's ArrayType
> before the declaration of the array.
>
> For example:
>
> 1. var x : Array<String> = [ "foo", "bar", document.title,  
> getAnotherString() ];

Array is not a parameterized type. If it were, the syntax would be  
Array.<T>.

Array must be backward compatible, so if it were changed to be  
parameterized by element type, then it would have to default to  
Array.<*> when used without a parameter list. But we don't have  
default type parameters, and anyway a type-parametric Array reference  
without the actual type param list would be ambiguous: should it mean  
Array.<*> by default, or Array, with instantiation using an actual  
type param happening later: let A = Array; type TA = A.<T> ?

So there's no good way to overload Array as you want.

Also, Array is closer to Object than to any homogenous tuple or  
vector type. Hence the separate

http://wiki.ecmascript.org/doku.php?id=proposals:arrays
http://wiki.ecmascript.org/doku.php?id=proposals:vector

proposals.

> 2. var x = [ "foo", "bar", document.title, getAnotherString() ] :  
> String;
>
> example 1 is more clear to read because in a long array, you'd see
> right away what type of Array it is.

Please see the above proposals and the prior proposals:

http://wiki.ecmascript.org/doku.php?id=spec:type_system
http://wiki.ecmascript.org/doku.php?id=spec:type_relations

> Is the former syntax (or some variant thereof) going to be available?

No, as explained above. But what you might want is:

let x : [string] = ["foo", "bar", document.title, getAnotherString()];

Notes:
* let trumps var in any hand ;-)
* string is the type of "foo", String is the dynamic class for  
wrappers, as in ES1-3 that shares its .prototype with string  
instances as well as String instances, via the usual prototype-based  
delegation. Prefer string in general.
* [string] is a type expression denoted a structural array type.

> What about for Objects?

See structural object types.

/be

_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss