tc39 / proposal-class-fields

Orthogonally-informed combination of public and private fields proposals
https://arai-a.github.io/ecma262-compare/?pr=1668
1.72k stars 113 forks source link

Arrow function shorthand #243

Closed edwjusti closed 5 years ago

edwjusti commented 5 years ago

I don't know if this question fits this proposal and repository, or if it's already been discussed before but it is closely related to class fields. But would it be possible to add a shorthand for fields declarations that use arrow functions like in dart so that we could omit the the assignment operator with the field if we are declaring an arrow function?

class Example {
  // main = () => 10
  main() => 10
  // fetchData = async () => '...' 
  async fetchData() => '...'
}
bakkot commented 5 years ago

I don't think that saving a single character is enough justification for a new syntactic form.

edwjusti commented 5 years ago

I agree that in terms of characters you don't save much with that syntax, but I found it a lot clearer that way especially with async functions or maybe even getters and setters.

mbrowne commented 5 years ago

People are already using thefetchData = async () => and have become accustomed to it. Having two different syntaxes that are about equally concise and do exactly the same thing is likely to be more confusing than helpful, and would also introduce further possibility of confusion with regular method syntax.

edwjusti commented 5 years ago

I understand, this is just a personal preference and can possibly introduce confusion among other developers. In my mind I imagined being able to use that syntax for more things besides async functions, like getters & setters in one line, or async iterators with lexical scope. But this would require other semantics and behavior changes in order to add that syntax with those other features which differs from regular class fields.