tc39 / proposal-extensions

Extensions proposal for ECMAScript
MIT License
150 stars 6 forks source link

Optional chaining for extensions -- `?::` #16

Open andyearnshaw opened 1 week ago

andyearnshaw commented 1 week ago

It would be great if optional chaining was baked into the proposal:

document.querySelector("#myElement")?::let(it => {
  // it's safe to do things with the element here
  setupInteractivity(it);
});

Equivalent to:

let temp = document.querySelector("#myElement");
if (temp != null) {
    setupInteractivity(temp);
}
hax commented 1 week ago

Yes, the proposal will be updated to include it. Especially I plan to change the syntax from :: to dot-based syntax, so it's very natural to still use ?. symbol, eg. document.querySelector("#myElement")?.do:let(it => { ... }).

hax commented 1 week ago

Note, one consequence of including optional chaining is we must simplify the hooks semantic. In the initial proposal, we have invoke hook which can skip the method resolving step (this help some use cases like eventual send proposal), but ?. semantic is based on that. Consider eventual send proposal itself also changed in past years and seems can't fully covered by extension proposal, I'd like to drop invoke hook and keep extension methods using similar semantic with normal methods and private methods.

ljharb commented 1 week ago

fwiw, obj?.do: seems like it'd conflict with condition ? obj?.do : …, parsing-wise.

hax commented 1 week ago

Yeah, the separator also need to be reinvestigated.

Options: