arthurfiorette / proposal-safe-assignment-operator

Draft for ECMAScript Error Safe Assignment Operator
https://arthur.run/proposal-safe-assignment-operator/
MIT License
1.41k stars 14 forks source link

Backward compatible implementation #1

Closed alfredosalzillo closed 2 months ago

alfredosalzillo commented 2 months ago

Interesting proposal but I have some concerns about the backwards compatibility of the proposal.

Implemented using a new symbol doesn't mean it can be used within existing code? As I understand the operator can only be called or have an effect on the code using the new symbol.

Would it work implemented as follow:


const [error, result] ?= doSomething()

// being transpilled in something like

const [error, result] = goify(() => doSomething())

Where goify is simply implemented for example as follow


function goify(callback) {
  try {
     const result = callback()
     return [null, result]
  catch (error) {
     return [error, null]
 }

Implemented like this for all the cases (object, function, promise, function returning a promise) will make the operator a sugar syntax compatible with existing code.

arthurfiorette commented 2 months ago

When targeting an older version, the same way that the transpiler would change the ?= syntax it would also register the symbol globally. Also, it would waste a lot of memory by creating another function.

Symbols are how new features are being implemented and backported as well, like https://github.com/tc39/proposal-explicit-resource-management