I know this was already discussed here but the "major mentality" JS of devs in 2024 (if I can even say that) is evolving towards a more safe approach. As we can observe with recent type stripping proposal and feature flag on nodejs, which 5-10 years ago the same proposal would've been instantly closed.
With all that said, I would like the opinion of this forum as well if I there's any champion that would like to support me with it.
Worded like that, it won't work. A function is an object. You cannot define safe assignment as acting on a value on the right-hand side. You only get a value if there was no error.
Your first example evaluates the right-hand side, then calls its [Symbol.result]
What would the following do?
function fun(a, b) { return a / b }
const [e, v] ?= action
Your second example shows a new way of evaluating a function-call expression:
Which could work, but you're allowing any error -- that happens before the function gets invoked -- to escape. And you'd also need a way to pass this through.
[e, v] ?= x.y.m(a, b.c.d, f())
You cannot call this safe assignment, when it can blow up in 5 other places before the method gets called.
In the end you'll realize that previous attempts at try-expression or the like are much more well-rounded -- they don't have any of these pitfalls and can be used anywhere, not just in assignment and variable declarations.
Why assignment is necessary to "try" something, meaning ignoring the error in some cases. I like the try keyword as it was proposed back then in 2019. E.g. try something() where I don't care about return value or error. I just want this line not to throw so my program continues
I like the idea of using both try keyword or ?= operator, anyways ?= can have even more benefits, since there is no way to have an error of using try await fn() or await try fn() which will definitely break ESLint, Babel and Typescript.
About using
try foo()
While it has benefits, I have no such constructions in my code, it almost never happens, and better to check for an error, this is definitely a good practice.
Why is a protocol desirable? This seems like if itβs anything but sugar for a try/catch, itβd be confusing.
This makes less code, less scopes, easier to read in a similar to await way, so yes it is sugar, it adds ability to write less code in a linear way.
By the way, both variants try and ?= are supported by Goldstein.