|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
ES4 draft: array initializer expressionssyntax, which has evolved quite a bit in ES4. The draft feels a little raw, maybe because we have yet to pin down all aspects of how structural array types interact with everything; please read critically and ask difficult questions. (The draft contains an informative section on structural array types as I think they ought to behave.) --lars Array initializer syntax
NAME: "Array initializer syntax"
FILE: spec/language/array-literals.html
CATEGORY: Expressions (E262-3 chapter 11)
SOURCES: ES3; REFERENCES [1]-[7]
SPEC AUTHOR: Lars
DRAFT STATUS: DRAFT 1 - 2008-04-08
REVIEWED AGAINST ES3: NO
REVIEWED AGAINST ERRATA: YES
REVIEWED AGAINST BASE DOC: NO
REVIEWED AGAINST PROPOSALS: NO
REVIEWED AGAINST CODE: NO
REVIEWED AGAINST TICKETS: YES
IMPLEMENTATION STATUS: ?
TEST CASE STATUS: ?
OPEN ISSUES
* A splicing shorthand has been introduced for ES4 array
initializers. In order to be compatible with other uses of the
same syntax (destructuring, rest arguments, the spread operator)
only one splice is allowed. That may be a foolish consistency.
REFERENCES
[1] proposals:structural_types_and_typing_of_initializers
[2] ES4 base document
[3] Ticket #66
[4] Ticket #120
[5] Ticket #249
[6] Ticket #319
[7] Ticket #370
SynopsisThis draft pulls together everything that has been proposed and tentatively agreed about array initializer syntax and semantics. Primary syntax In its general form an array initializer is comprised of an
optional keyword ( [ "const" | "var" ] "[" Body "]" [ ":" Type ]where Body is one of AssignmentExpression? ( "," AssignmentExpression? )* ( AssignmentExpression? "," )* "..." AssignmentExpression The "Type" that annotates the initializer must be a structural array type or a nominal class type whose constructor accepts zero arguments. Creating Array objectsIf the annotating type is absent, or if the annotating type is a structural array type, then the following rules apply. NOTE Some informative remarks about structural array types are included at the end of the document. As in ES3, an elided expression leaves a hole in the array (by the same syntactic rules, not repeated here). NOTE The value of a hole can be read provided the type of the hole
has a default value (as do primitive string, number, and boolean
values as well as nullable object types and the "any" type The final optional expression of the form FIXME The above needs to be formalized properly, obviously. If the array initializer is prefixed by The general form of Type is NOTE If the initializer is prefixed by Creating arbitrary objects with numbered properties If the annotating type is a class type whose constructor accepts
zero arguments then the In this case, the expression evaluates to a new instance of the
class type where successive initializer values have been assigned to
numbered properties of the new object and the length of the
initializer has been assigned to the In other words,
[1,,3,,] : Vector.<double>
is syntactic sugar for
(let (TMP = new Vector.<double>)
(TMP[0] = 1,
TMP[2] = 3,
TMP.length = 4,
TMP))
for a fresh variable TMP.
Secondary syntaxSuppose T is a structural array type: type T = [...double] Then the new T( 10 ) new T( 5, 1.0 ) The meaning of the above two phrases is exactly: [,,,,,,,,,,] : T [1.0, 1.0, 1.0, 1.0, 1.0] : T In other words, used with a structural array type the NOTE The array type can be of any kind: with or without fixed types or a rest type. RationaleThis section is informational and will not be part of the finished Standard. The splicing syntax is symmetric with the prefix
var [x,y,...z] = E
To remain compatible with the destructuring syntax, the splicing syntax is only allowed at the end of the literal -- it doesn't make sense to have an open destructuring pattern in the middle of a pattern. However, that may be taking compatibility too far, as there are obvious uses for having multiple splices (just as there are uses for allowing multiple spread operators in a function call). The type Doubles = [...double] new Doubles(10)evolves easily to type Doubles = Vector.<double> new Doubles(10) Nominal type annotations on array initializers further the evolutionary programming agenda, allowing programs that use structural array types as annotations on literals to be changed easily to use class types instead. For example, type Doubles = [...double] [1,2,3]:Doublesevolves easily to type Doubles = Vector.<double> [1,2,3]:Doubles In addition, Structural array typesThis section is informational; a separate spec will explain array types in detail. A structural array type is comprised of some possibly empty
sequence of fixed types T0 through Tk followed by an
optional rest type Tr. Elements in the type can be elided;
elided elements default to the "any" type
"[" ( Type? "," )* ("..." Type)? "]"
Examples:
[...double] /* doubles, length >= 0 */
[string, ...double] /* one string followed by doubles; length >= 1 */
[string, double] /* one string and one double, length = 2 */
[string, , string] /* [string, *, string] */
[] /* length = 0 */
An object v is said to be of an array type T if v is
an instance of (a subtype of) The fixed type constraints are handled by means of fixtures: fields named 0 through k are fixtures. The general constraint Tr on the other hand applies to dynamic properties. Effectively (and making use of some imaginary syntax), we can say that [string, double, ...boolean]is essentially this record type:
{ 0: string, 1: double, 2...: boolean, length: double }
(apart from a lot of methods defined on Array objects).
This split between fixture and dynamic properties looks like it could cause trouble, but as it happens it will not. The reason is that it could cause trouble is that there is a two-pass name lookup algorithm that gives priority to fixture properties, and that means that in the above object the property 1 and the property 2 would be treated differently though both are logically array properties. That would seem bizarre. The reason that it does not cause problem in practice is that the
difference between fixture and dynamic properties is not observable in
an object that has been created by annotating array initializers with
a structural array type. Such an object would have
The final wrinkle (alluded to earlier) is that fixture properties may be elided from the initializer. For example, consider this object: ["foo", ,] : [string, double] The property "1" can't be missing, because the type requires that
it be there: the object "is of that type", so by definition the
property "1" must always exist, and the type of that property must
always be There is a simple interpretation for this (also alluded to
earlier): the property is in the object, but it is uninitialized. If
the type of the property has a default value, then that value is
produced when the property is read. Otherwise, the property cannot be
read before it has been written (or a run-time error occurs). That
happens for instances of non-nullable user-defined classes.
|
|
|
Re: ES4 draft: array initializer expressionsMy comments:
The grammar is ambiguous. var [x, y] = expr; is both an assignment expression statement and a destructuring variable binding statement. How does the "length" property work for structural type literals? Is it always a fixture? > If the array initializer is prefixed by |const| or |var| then the array > properties created will be created as fixtures; if the prefix is |const| > those fixtures will additionally be read-only. Am I to read this as stating that the length is also a constant if "const" is used? It's a bit unclear. Waldemar _______________________________________________ Es4-discuss mailing list Es4-discuss@... https://mail.mozilla.org/listinfo/es4-discuss |
|
|
Re: ES4 draft: array initializer expressions'var' and 'const' need to be added to the lookahead set on
ExpressionStatement. That will take care of the ambiguity. Jd On 4/15/08 5:08 PM, Waldemar Horwat wrote: > My comments: > > > The grammar is ambiguous. > > var [x, y] = expr; > > is both an assignment expression statement and a destructuring variable > binding statement. > > > How does the "length" property work for structural type literals? Is it > always a fixture? > > >> If the array initializer is prefixed by |const| or |var| then the array >> properties created will be created as fixtures; if the prefix is |const| >> those fixtures will additionally be read-only. > > Am I to read this as stating that the length is also a constant if "const" is > used? It's a bit unclear. > > > Waldemar > _______________________________________________ > Es4-discuss mailing list > Es4-discuss@... > https://mail.mozilla.org/listinfo/es4-discuss _______________________________________________ Es4-discuss mailing list Es4-discuss@... https://mail.mozilla.org/listinfo/es4-discuss |
|
|
Re: ES4 draft: array initializer expressionsI have a (perhaps stupid) question regarding the Array type syntax. What is the use case for a type like:
[string, double, ...boolean] I understand the utility of heterogeneous tuples of a fixed size, and of homogeneous lists of variable size, but I don't really see when I would use a hybrid like this. What am I missing? regards Olav Junker Kjær _______________________________________________ Es4-discuss mailing list Es4-discuss@... https://mail.mozilla.org/listinfo/es4-discuss |
| Free Forum Powered by Nabble | Forum Help |