object literal types

# Michael Haufe (17 years ago)

I was reading through the wiki on the type discussions:

strawman:types, discussion:object_literals_type_of

I realize that some of the content is dated and does not apply to Harmony in particular, but I'm curious to what the proposal is for inline types declarations on object literals

This example (from the second url) is straightforward enough:

type Goose = {name: string, email: string, noisy: boolean}; // create a Goose and pass it to f() f({name: 'Ben', email: 'ben at quack.net', noisy: true} : Goose);

But this example seems a bit lacking:

var white = {r: 0xff, g: 0xff, b: 0xff}; // the type is {}, not {r:, g:, b:, *} or anything like that.

Is there a problem with the following syntax or did I overlook some implication/discussion?

var Point:object = { color:string x:double = 20, y:double = 50, z:double = 3 }

# Michael Haufe (17 years ago)

var Point:object = { color:string x:double = 20, y:double = 50, z:double = 3 }


Sorry, typo correction:

var Point:object = { color:string = "red", x:double = 20, y:double = 50, z:double = 3 }

# David-Sarah Hopwood (17 years ago)

Michael Haufe wrote:

var Point:object = { color:string x:double = 20, y:double = 50, z:double = 3 }


Sorry, typo correction:

var Point:object = { color:string = "red", x:double = 20, y:double = 50, z:double = 3 }

':' (not '=') is used to separate a property name from its value, so it can't also be used for type annotations. This example would have to be something like:

var Point = { color @string: "red", x @double: 20, y @double: 50, z @double: 3, };

# Michael Haufe (17 years ago)

David-Sarah Hopwood wrote:

"':' (not '=') is used to separate a property name from its value, so it can't also be used for type annotations."

Except in the case of JavaScript's non-standard Sharp Variables (developer.mozilla.org/En/Sharp_variables_in_JavaScript), which is what sparked my question.Is there some ambiguity on why this syntax reuse would be off the table?

# David-Sarah Hopwood (17 years ago)

Michael Haufe wrote:

David-Sarah Hopwood wrote:

"':' (not '=') is used to separate a property name from its value, so it can't also be used for type annotations."

Except in the case of JavaScript's non-standard Sharp Variables (developer.mozilla.org/En/Sharp_variables_in_JavaScript), which is what sparked my question. Is there some ambiguity on why this syntax reuse would be off the table?

I don't see how the sharp-variables extension is relevant. If anything it is another reason to avoid '=' in property assignments, so that there is no conflict between that extension and ES-Harmony types.

# P T Withington (17 years ago)

On 2009-02-17, at 08:07EST, Michael Haufe wrote:

JavaScript's non-standard Sharp Variables (developer.mozilla.org/En/Sharp_variables_in_JavaScript )

I had no idea SpiderMonkey was implemented in Lisp!

# Brendan Eich (17 years ago)

On Feb 17, 2009, at 5:07 AM, Michael Haufe wrote:

David-Sarah Hopwood wrote:

"':' (not '=') is used to separate a property name from its value,
so it can't also be used for type annotations."

Except in the case of JavaScript's non-standard Sharp Variables (developer.mozilla.org/En/Sharp_variables_in_JavaScript ),

Sharp variables (which I modeled after Common Lisp) are different
syntax -- their = sign comes before the object-type property value
or outermost object or array initialiser, and the = is preceded by the
sharp variable name. Also, you can't have any spaces between #n and =
(for non-negative integer n). There's really no comparison with the
syntax you sketched.

which is what sparked my question.Is there some ambiguity on why
this syntax reuse would be off the table?

Yes, there is an ambiguity. See the grammar:

PropertyNameAndValueList: PropertyName ':' AssignmentExpression PropertyNameAndValueList ',' PropertyName ':' AssignmentExpression

# Michael Haufe (17 years ago)

Brendan Eich wrote:

On Feb 17, 2009, at 5:07 AM, Michael Haufe wrote:

David-Sarah Hopwood wrote:

"':' (not '=') is used to separate a property name from its value, so it can't also be used for type annotations."

Except in the case of JavaScript's non-standard Sharp Variables (developer.mozilla.org/En/Sharp_variables_in_JavaScript),

Sharp variables (which I modeled after Common Lisp) are different syntax -- their = sign comes before the object-type property value or outermost object or array initialiser, and the = is preceded by the sharp variable name. Also, you can't have any spaces between #n and = (for non-negative integer n). There's really no comparison with the syntax you sketched.

which is what sparked my question.Is there some ambiguity on why this syntax reuse would be off the table?

Yes, there is an ambiguity. See the grammar:

PropertyNameAndValueList: PropertyName ':' AssignmentExpression PropertyNameAndValueList ',' PropertyName ':' AssignmentExpression

/be

Just the explanation I was looking for,