arthurfiorette / proposal-safe-assignment-operator

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

Syntax overkill in basic example #7

Closed Akiyamka closed 3 weeks ago

Akiyamka commented 1 month ago

Prerequisites

Versions

not related

A minimal reproducible example

https://stackoverflow.com/help/minimal-reproducible-example.

Description

not related

Steps to Reproduce

not related

Expected Behavior

instead of

const [error, result] ?= example()

function example() {
  [Symbol.result]() {
    return [
      new Error("Error"),
      null
    ]
  },
}

you can write just

const exampleBox = (...args) => example(...args) ?? [new Error("Error"), null]
const [error, result]  = exampleBox()
arthurfiorette commented 1 month ago

Yes the syntax is overkill for this super simple surrealistic example. I put it there to showcase how it works in objects, later meant to be used as class/object properties in which you don't know when and where the function is going to be called.