Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Alternative syntax #50

Closed
PFight opened this issue Mar 5, 2018 · 6 comments
Closed

Alternative syntax #50

PFight opened this issue Mar 5, 2018 · 6 comments
Labels
alternative syntax past ideas and discussions about alternative syntaxes duplicate

Comments

@PFight
Copy link

PFight commented Mar 5, 2018

Syntax of function call (foo?.()) and accessing array element (array?.[i]) is quite uggly. The reason as known - problems with parsing, because of ternary operator ? :, so we can't use foo?() and array?[i].

I suggest change syntax a bit, and make different operator ?? for function calls and array access.

  1. Member access: user?.address?.street - no changes
  2. Function call: myForm.checkValidity??() instead of myForm.checkValidity?.()
  3. Array access: myArray??[i] instead of myArray?.[i]

It's looks nice and has no problem with parsing. What do you think?

@ljharb
Copy link
Member

ljharb commented Mar 5, 2018

Having all three of those not be consistent is a problem; they’re conceptually the same kind of operator.

@PFight
Copy link
Author

PFight commented Mar 5, 2018

Another variant, change point 1 too:

user??.address??.street

@PFight
Copy link
Author

PFight commented Mar 5, 2018

@ljharb all these cases are not the same. What is operator ?. should mean? In first point it is accessing member (user?.address), but in second and third it is not accessing member. There we getting some value, and applying to it function call or array access.

myForm.checkValidity?.() is the (myForm.checkValidity?.) ()

So, if we are going to keep consistent, then we should write:

user?..address because it is (user?.).address

So, there is no consistence right now.

@ljharb
Copy link
Member

ljharb commented Mar 5, 2018

The current proposal is ?. ?[, and ?(, with nullish coalescing as ??.

After #48, the proposal will be ??., ??[, and ??(, and nullish coalescing will be ??:.

In both cases, all three optional chaining operators are <token>., <token>[, and <token>(. This is an important and necessary characteristic.

@PFight
Copy link
Author

PFight commented Mar 5, 2018

Hm, REAMDE is outdated...

@claudepache
Copy link
Collaborator

claudepache commented Mar 5, 2018

The current proposal is ?. ?[, and ?(, with nullish coalescing as ??.

Correction: it is currently ?., ?.[, and ?.(


Closing this as a duplicate of #5 and/or #34.
As @ljharb mentioned, this is expected to be resolved by PR #48.

@claudepache claudepache added the alternative syntax past ideas and discussions about alternative syntaxes label Jun 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
alternative syntax past ideas and discussions about alternative syntaxes duplicate
Projects
None yet
Development

No branches or pull requests

3 participants