A common pattern for memoization is to check if a variable/property is undefined and, if not, return the value or, if so, calculate the value, and then assign/return it. The initial test is essentially the same as the tests that motivated optional chaining. But, why not extend optional chaining to return? Rather than:
if (this.#foo) return this.#foo;
we do
return this.#foo?
This seems like a very common pattern, couldn't possibly cause syntactic issues, since the ? would already be acceptable wherever this would be used (currently, always follwed by a chained property), and actually reads like what it means. I'm sure this must have been proposed before, but couldn't find it, so apologies in the likely case that this is a repeat.
Yup, fair enough point. That does make the proposed syntax non-viable as it essentially means one must end the proposed option return with a semicolon:
return this.#foo?;
which, of course, isn't true for any other syntactic elements, AFAIK (best practices are to end every statement with a semicolon but that's irrelevant, of course). Plus the ?; doesn't look that nice, anyway.
I guess the return? that aclaymore suggested might be more syntactically viable but that seems like a more significant change to the language (I had wrongly viewed my proposal as just a minor tweak to optional chanining). Almost certainly not worth the minor benefit.
I haven't done a lot of research on how common this pattern is elsewhere. I seem to hit it a lot, myself, and I guess I figured that if it were common there'd be a groundswell of support for the proposal. ;-) Maybe that's a bad way to use this discussion group? If so, sorry. It seemed to be a convenience in the spirit of optional chaining though, admittedly, less common.
In any case, Tab Atkins-Bittner pointed out a fatal flaw in the proposed syntax so not worth pursuing this any further.