News for hackers. Stuff that matters.
Re:ES6 in Depth: Symbols (2015) by ajanuary on Wednesday July 12, 2017 @07:03AM (#14751510)
It's also not really true: they're the opposite of symbols in lisp and ruby. In lisp and ruby, `:a == :a` is true, while in JavaScript `Symbol("a") == Symbol("a")` is false.
Re:ES6 in Depth: Symbols (2015) by TeMPOraL on Wednesday July 12, 2017 @07:09AM (#14751535)
They are similar, but with kind of inverted defaults.

Symbol("a") == Symbol("a") is more-less equivalent to Common Lisp's (eq (make-symbol "a") (make-symbol "a")), both of which will return false, because (in Lisp terms) we're creating two different symbols. In Common Lisp, the default behaviour of a reader if it sees a stand-alone symbol token, is to intern it - put it in "global"[0] storage, so that if the reader sees the same-looking symbol token again, it returns the same symbol instance it previously interned.

In JavaScript, per this article, the interning behaviour is available through Symbol.for(), i.e. Symbol.for("a") == Symbol.for("a") should return true, much like (eq 'a 'a) would return true in Lisp.

--

[0] - it's actually per-package storage, but that's getting into the more advanced details of CL.

--

EDIT replaced gensym with make-symbol in Common Lisp example, as the former is used primarily in writing macros, so it ensures the created symbol has an unique printed representation, which helps make macroexpansions more readable.

  (make-symbol "FOO") -> #:FOO
  (make-symbol "FOO") -> #:FOO ; NOT eq to the previous one
  (gensym "FOO") -> #:FOO103
  (gensym "FOO") -> #:FOO104 ; note the incrementing counter appended by gensym
Compare http://clhs.lisp.se/Body/f_mk_sym.htm and http://clhs.lisp.se/Body/f_gensym.htm.
Re:ES6 in Depth: Symbols (2015) by Touche on Wednesday July 12, 2017 @08:04AM (#14751773)
But in Ruby people use symbols to specify an object's properties, and they aren't used that way in JavaScript at all. In JavaScript they are used for metadata and other things you don't want to be serialized by JSON.stringify() or Object.assign(). I've always been confused by why Ruby uses symbols so much.
Re:ES6 in Depth: Symbols (2015) by masklinn on Wednesday July 12, 2017 @08:56AM (#14752077)
> In JavaScript they are used for metadata and other things you don't want to be serialized by JSON.stringify() or Object.assign().

They're used to avoid conflicts for "generic method" hooks (similar to dunder methods in Python): the conflict issues is why environments have broken when new methods were added, and why e.g. Object.keys and their ilk are "static" functions rather than methods.

If you don't want JSON.stringify or Object.assign to see your properties just make them non-enumerable e.g.:

    > const obj = {};
    > Object.defineProperty(obj, 'prop', {'value': 42});
    > obj
    < {prop: 42}
    > JSON.stringify(obj)
    < "{}"
    > Object.assign({}, obj)
    < {}
Object.assign will copy symbol-keyed properties. In fact Symbol-keyed properties are enumerable by default.

They live in an odd half-way world because many constructs only accept/handle string-keyed properties: Object.keys/entries/values and JSON.stringify use EnumerableOwnProperties and (for..in) uses EnumerateObjectProperties, both of which are specified to ignore non-string-keyed properties (before even checking for enumerability).

> I've always been confused by why Ruby uses symbols so much.

Mutable non-interned strings.

Re:ES6 in Depth: Symbols (2015) by mnarayan01 on Wednesday July 12, 2017 @09:45AM (#14752448)
> I've always been confused by why Ruby uses symbols so much.

Good: Other people mainly covered it. Also note that when writing extensions using the C API, accepting options as symbols is generally much more convenient.

Less good:

  val = "something"
  val = :something
  # Saves one character and no need for an end token.

  { "key" => val }
  { key: val }
  # Saves four characters.
Re:ES6 in Depth: Symbols (2015) by nv-vn on Wednesday July 12, 2017 @08:20AM (#14751856)
Well, Ruby objects are basically big hash tables in a way. Makes sense to use a prehashed string to index them (if I understand how symbols work correctly).
Re:ES6 in Depth: Symbols (2015) by yomly on Wednesday July 12, 2017 @08:40AM (#14751964)
Using symbols I think is more expressive than using strings.

I like the separation of strings for external data and symbols for internal data.

So symbols will usually be used for enums or things which logic or control flow depend on. Symbols then have the benefit of feeling like syntax and the scheme zen of code is data is code...

Re:ES6 in Depth: Symbols (2015) by lixquid on Wednesday July 12, 2017 @07:29AM (#14751610)
Symbol.for("a") != Symbol.for("b")

Perhaps you meant Symbol.for("a") for both?

Re:ES6 in Depth: Symbols (2015) by TeMPOraL on Wednesday July 12, 2017 @07:31AM (#14751617)
That's what I meant. Thanks! I corrected the typo.
Re:ES6 in Depth: Symbols (2015) by sim0n on Wednesday July 12, 2017 @07:08AM (#14751529)
FWIW, you can use `Symbol.for` to reuse symbols in JS (`Symbol.for("a") === Symbol.for("a")` is true).
Re:ES6 in Depth: Symbols (2015) by masklinn on Wednesday July 12, 2017 @07:12AM (#14751546)
> In lisp and ruby, `:a == :a` is true, while in JavaScript `Symbol("a") == Symbol("a")` is false.

But `Symbol.for("a") === Symbol.for("a")`, and Symbol("a") is very similar to (make-symbol "a").

Re:ES6 in Depth: Symbols (2015) by gvx on Wednesday July 12, 2017 @07:50AM (#14751708)
I'm designing a programming language that has symbols that are namespaced to objects, where two symbols are equal iff the object they're linked to is the same and their string representation is the same.

In that language, the equivalent of :a == :a would be true, and you would use different "namespace objects" to prevent collisions.

For example, classes that implement the function interface could provide methods named call and partial, that are namespaced to the function class object, leaving you free to define methods with those names but different namespaces for completely different purposes.

Re:ES6 in Depth: Symbols (2015) by acjohnson55 on Wednesday July 12, 2017 @09:21AM (#14752255)
Why is that helpful? In JS, the way you accomplish the same thing is to assign the single instance of the symbol to a property in your module, and that's how you access it, rather than summoning it with the Symbol function. Not sure what the machinery you're suggesting buys you, but I could be missing something!
Re:ES6 in Depth: Symbols (2015) by gvx on Thursday July 13, 2017 @02:40PM (#14763952)
I don't suppose it buys you anything JS can't do, but it does allow those things to be integrated closely in the language, in a way JS can't do without majorly breaking the language.
Re:ES6 in Depth: Symbols (2015) by _pmf_ on Wednesday July 12, 2017 @07:08AM (#14751531)
That's perfectly in line with web devs appropriating and perverting well established terms due to a mix of willful ignorance and cultivated arrogance.