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

Clarify behavior of 'o?.m()' #16

Closed
rbuckton opened this issue Jul 22, 2017 · 5 comments
Closed

Clarify behavior of 'o?.m()' #16

rbuckton opened this issue Jul 22, 2017 · 5 comments

Comments

@rbuckton
Copy link

In C#, o?.m() returns null if o is null and does not throw. Given that in this proposal we have f?.(), which returns undefined if f is undefined, what is the expected behavior of o?.m() in this proposal?

  1. o?.m() returns undefined if o is undefined.
  2. o?.m() throws if o is undefined. To avoid the error, you must write o?.m?.().

If this proposal were to use the exact semantics of C#, then f?.() would not be legal, and you would instead need to write f?.call(). I'm not saying we should use the semantics of C#, just that developers from other languages with experience with this feature may intuitively believe that (1) is the correct behavior.

@rbuckton
Copy link
Author

If I am following EvaluateCall correctly, it looks like (1) above is the correct interpretation according to 2.a.i. of the algorithm steps. It would be worth adding this example to https://github.com/tc39/proposal-optional-chaining#semantics.

@jridgewell
Copy link
Member

See #2.

@rbuckton
Copy link
Author

Thanks. I still think it may be worth adding to the examples in README.md as its a common enough case.

@claudepache
Copy link
Collaborator

claudepache commented Jul 24, 2017

@rbuckton
The README has already the following example: a?.b.c().d. But yes, there should be more explanations.

You can check the README of my own repo for more detailed explanations (https://github.com/claudepache/es-optional-chaining), although the “official” feature may have some differences.

@claudepache
Copy link
Collaborator

@rbuckton The updated README must answer your question. Please check and say if it is OK.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants