Skip to content

util: logging symbols should convert using String(symbol) #927

Closed

Description

@domenic
var symbol = Symbol("My symbol");

console.log(symbol); // fine, logs Symbol(My symbol)
console.log("foo", symbol); // throws

Output:

$ iojs test.js
Symbol(My symbol)
util.js:34
      str += ' ' + x;
                 ^
TypeError: Cannot convert a Symbol value to a string
    at Console.exports.format (util.js:34:18)
    at Console.log (console.js:36:34)
    at Object.<anonymous> (c:\Users\Domenic\Dropbox\GitHub\whatwg\streams\reference-implementation\test.js:4:9)
    at Module._compile (module.js:444:26)
    at Object.Module._extensions..js (module.js:462:10)
    at Module.load (module.js:339:32)
    at Function.Module._load (module.js:294:12)
    at Function.Module.runMain (module.js:485:10)
    at startup (node.js:111:16)
    at node.js:799:3

Note that the correct way to stringify a symbol is with the explicit conversion, String(symbol), instead of implicitly with the + sign.

I can get to this after work if nobody else does first.

Activity

  1. vkurchatkin commented on Feb 23, 2015

    @vkurchatkin
    Contributor

    Is it the correct behaviour though? I thought it was v8 bug

  2. added
    utilIssues and PRs related to the built-in util module.
    on Feb 23, 2015
  3. domenic commented on Feb 23, 2015

    @domenic
    ContributorAuthor

    Yeah, this is correct per spec and several TC39 meetings. It is dangerous to allow symbols to be implicitly converted to strings (and in some cases you just flat out can't do it, e.g. obj[prop] usually does an implicit string conversion). However explicit conversion is fine.

  4. cjihrig commented on Feb 24, 2015

    @cjihrig
    Contributor

    Closed by ed3b057

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    utilIssues and PRs related to the built-in util module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions