tc39 / proposal-explicit-resource-management

ECMAScript Explicit Resource Management
https://arai-a.github.io/ecma262-compare/?pr=3000
BSD 3-Clause "New" or "Revised" License
758 stars 30 forks source link

Any chance for an optional name binding? #214

Closed itsMapleLeaf closed 7 months ago

itsMapleLeaf commented 7 months ago

I've found a few cases where I want to use using purely for executing a side effect within a scope. Here are some examples:

test("something", () => {
    using _ = mockTimers()
    // ideal: using mockTimers()

    doSomeTests()
})

function mockTimers() {
    vi.useFakeTimers()
    return {
        [Symbol.dispose]() {
            vi.useRealTimers()
        }
    }
}
{
    // atm, I have to use underscored names for unused variables
    using _someDependency = provideSomeDependency()
    using _someOtherDependency = provideSomeOtherDependency()

    // ideal:
    // using provideSomeDependency()
    // using provideSomeOtherDependency()

    doSomething()
}

function doSomething() {
    useSomeHelper()
}

function useSomeHelper() {
    const someDependency = useSomeDependency()
    const someOtherDependency = useSomeOtherDependency()
}
MadProbe commented 7 months ago

As far as I can remember there was using void = ... syntax for specifying a using without a name, but I don't know why it was removed.

senocular commented 7 months ago

Void bindings are in a separate proposal now

https://github.com/tc39/proposal-discard-binding

rbuckton commented 7 months ago

Yes, this achieved Stage 1 in the February 2024 plenary session.