erights / Orthogonal-Classes

Proposed EcmaScript Class Syntax clarifying orthogonal concerns
29 stars 2 forks source link

Consistency with object literal syntax #12

Closed bathos closed 7 years ago

bathos commented 7 years ago

Right now, class syntax mirrors object literal syntax in terms of defining methods and accessors, which seems really elegant to me since it makes things intuitive and predictable, and also hints at what a class definition really is:

const obj = { foo() {} }; // object with foo method
const cstr = class { foo() {} }; // class whose prototype has foo method

Syntactically the main differences are that object literals require commas, that class bodies permit semicolons, and that object literals permit shorthand properties. That the static modifier is available only in class syntax makes sense, since it is only applicable there.

I’ve been wondering why this is not also the approach being taken with non-method, non-accessor properties of the prototype, constructor (and now instance). That is, is there a reason to prefer this:

const obj = { foo: true };
const cstr = class { foo = true; };

over this?:

const obj = { foo: true };
const cstr = class { foo: true };

Maybe there are grammar issues I’m missing, but these are the advantages I think it has:

littledan commented 7 years ago

A lot of us (including me) would be very happy if we could use : for initializing property declarations. Unfortunately, this is "reserved" for systems like TypeScript. which use the exact same syntax for type declarations. This is sort of codified in the fifth bullet point of Forbidden Extensions:

The Syntactic Grammar must not be extended in any manner that allows the token : to immediately follow source text that matches the BindingIdentifier nonterminal symbol.

bathos commented 7 years ago

Wow, thanks for the answer. I am pretty surprised to learn this is a codified constraint given how (relatively) small and young those non-standard extensions are. (It's a little disheartening to entertain cynical thoughts about what this might suggest about priorities.) Closing since it’s a lost cause.