proposal-extensions icon indicating copy to clipboard operation
proposal-extensions copied to clipboard

Optional chaining for extensions -- `?::`

Open andyearnshaw opened this issue 1 year ago • 4 comments

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);
}

andyearnshaw avatar Sep 26 '24 13:09 andyearnshaw

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 avatar Sep 26 '24 18:09 hax

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.

hax avatar Sep 26 '24 18:09 hax

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

ljharb avatar Sep 26 '24 18:09 ljharb

Yeah, the separator also need to be reinvestigated.

Options:

  • obj.ext@method() obj?.ext@method()
  • obj.ext#method() obj?.ext#method()
  • obj.ext~method() obj?.ext~method()
  • obj.ext!method() obj?.ext!method()
  • obj.ext'method() obj?.ext'method()
  • obj.ext::method() obj?.ext::method()

hax avatar Sep 27 '24 02:09 hax