Skip to content

using.target to detect when in the expression of a using declaration. #271

@Qard

Description

@Qard

This is addressing the same issue as #159 but with a very different proposed solution so I figured a separate thread might make more sense.

My thinking is to have an equivalent to new.target but for detecting when executing within the expression given to a using declaration.

function makeUsable() {
  if (!using.target) {
    throw new Error('must be used!')
  }

  return {
    [Symbol.dispose]() {
      console.log('disposed')
    }
  }
}

using foo = makeUsable() // fine
const foo = makeUsable() // throws

Probably should only work for direct calls or constructions as there's a bunch of complications like what happens in nested calls or if there are branching expressions like a() && b() where it could be that both are producing a usable value but only one would actually reach the declaration. Probably lots more edge cases I'm not thinking of right now, but it's an idea anyway. Could help to at least make ERM a bit safer for those that opt to provide usable values.

This came up in reference to: nodejs/node#61674

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions