Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Discussion with TC39 about the semantics of symbols

dherman
September 18, 2013

Discussion with TC39 about the semantics of symbols

I presented these slides to TC39 in Sept 2013 to lead a discussion about the semantics of ES6 symbols.

dherman

September 18, 2013
Tweet

More Decks by dherman

Other Decks in Programming

Transcript

  1. Text
    Dave Herman
    September 18, 2013
    SYMBOLS: OBJECT OR PRIMITIVE?

    View full-size slide

  2. 1. STATELESSNESS
    • Sharing a symbol should not share state.
    • Encapsulates a property key and nothing else.

    View full-size slide

  3. 2. CROSS-FRAME COMPAT
    obj[iterator] = function*() { ... };
    let w = new Window(...);
    w.shared = obj;

    View full-size slide

  4. alert.call()
    Math.sin(0)
    document.getElementById("body")
    (1.2).toFixed()
    "tc39".toUpperCase()
    true.toString()
    3. METHODS

    View full-size slide

  5. 4. MUTABLE PROTOTYPES
    Yes, monkey-patching in general is bad.
    But monkey-patching standard methods is a best
    practice.
    The evolution of the Web platform depends on it.

    View full-size slide

  6. SHALLOW-FROZEN OBJECTS
    O.gPO(iterator).foo = 12;
    Fails Desideratum #1: stateful
    Fails Desideratum #2: distinct xframe iterators

    View full-size slide

  7. DEEP-FROZEN OBJECTS
    O.gPO(iterator).foo = 12 // strict error
    Fails Desideratum #4: no evolution

    View full-size slide

  8. O.gPO(iterator) === null
    Fails Desideratum #3: no methods
    PROTOTYPE-FREE OBJECTS

    View full-size slide

  9. NON-WRAPPING PRIMITIVES
    iterator.valueOf() // error
    Fails Desideratum #3: no methods

    View full-size slide

  10. •JS already has an answer for this!
    •typeof iterator === "symbol"
    •Get/call operations auto-wrap
    •Prototype state is global per-frame
    •Sending across frames doesn’t share state

    View full-size slide

  11. • I know people think auto-wrapping is gross.
    • Here’s my positive spin:
    • Provides a uniform OO surface for all values.
    • Does so without ruining value immutability.
    • Does so without ruining API patchability.
    • Going forward: we need a solution for value types.
    YES, I DO SEE THAT ELEPHANT

    View full-size slide

  12. REMAINING ISSUES

    View full-size slide

  13. FOOTGUNS?
    [[ToPropertyKey]] of Symbol objects: auto-
    unwrap? Does it really matter in practice?
    Worry about toString for symbols and Symbol
    objects? Again, does it matter in practice?

    View full-size slide

  14. EXTENDING TYPEOF
    Do we know it won’t break the Web?
    MSIE "unknown" type may simply be rare enough
    to be undiscovered.
    Fallback: "object" with [[Get]] et al that behave
    like auto-wrappers? (plus Object.isValue()?)

    View full-size slide

  15. 18 SEPT 13 TC39 RESOLUTIONS
    • Yes to primitives with auto-wrapping
    • No auto-unwrapping of Symbol objects
    •typeof iterator === "symbol"
    • Symbol.prototype.toString should throw to
    help catch bugs in code evolution;
    Object.prototype.toString usable for
    infallible string coercion
    • Symbol() creates primitive, new Symbol throws

    View full-size slide