Skip to content

Defining Symbol.dispose and other symbol methods #60

@guybedford

Description

@guybedford

With explicit resource management becoming a common pattern in the JS ecosystem, it could be worthwhile to support defining a Symbol.dispose method on custom descriptors to associate custom destructor operations with a given GC object.

Has there been previous discussion of supporting this pattern of resource consumption when working with GC descriptor objects?

There are also other Symbol methods that could be useful to define - Symbol.asyncDispose, Symbol.iterator, Symbol.asyncIterator, Symbol.toStringTag, Symbol.hasInstance and Symbol.toPrimitive (full list - https://tc39.es/ecma262/#sec-well-known-symbols).

A general way to allow these methods could be to support all well known ECMA-262 symbols with either a special bit or a special prefix like @@wellKnownSymbol (the old ecma-262 naming convention for these symbols, new convention is %wellKnownSymbol%). There is then also the question if custom symbols should be supported via Symbol.for('foo'), where that would then need separate design as well. But well-known symbols is likely good enough.

So the main questions seem to be:

  • Is Symbol.dispose a use case worth supporting?
  • Is supporting all well-known symbols worthwhile, and therefore a general Symbol solution is preferable?
  • Should Symbol.for also be supported?

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