4 ms·
I'm not entirely convinced with the function-call syntax for declaring a Symbol. Why not using something more of the lines with Ruby's :symbol syntax?
by welfare 11y ago
I'm not entirely convinced with the function-call syntax for declaring a Symbol.
Why not using something more of the lines with Ruby's
:symbol
syntax?
- _pmf_ 11y ago> Why not using something more of the lines with Ruby's :symbol syntax? You mean Common Lisp's syntax, of course.
- Grue3 11y ago":symbol" is only syntax for keywords though. In general the syntax is "package:symbol" or just "symbol" (if it's in current package).
- mrighele 11y agoI guess because they do not behave in the same way. In fact new Symbol() is closer to Common Lisp's (gensym) than regular :symbol, since everytime you call it you get something unique. In particular object[:symbol] would behave quite differently (and would be probably worthless since everytime you would access a new property ). It would make more sense to have :symbol a shortcut for Symbol.for("symbol") instead, but I don't think it would be that much useful.
- masklinn 11y ago> Why not using something more of the lines with Ruby's :symbol syntax? Because Ruby's symbols are completely different and designed to collide. A Ruby symbol (like an Erlang atom) is a very cheap immutable string-like structure but :symbol is :symbol. A Javascript symbol is (as others noted) much closer to a lisp unique symbol (the output of `gensym`), and having it be a function is perfectly fine since you have to keep it around anyway (or you can't access symbol-named properties). Symbol.for is roughly equivalent to :symbol, but the use case for it is much more limited and there's really no reason for a literal version./
- carussell 11y agoHere's the other thing that neither of the other commenters mentioned: You can't go around peppering your code with use of this new syntax if you want it to continue working on older runtimes. With `Symbol`, you can at least create a polyfill that approximates what it's shooting for. > declaring a Symbol Don't think of it as "declaring a symbol" (because it isn't). Think of it as generating a symbol (because it is).
- hajile 11y agoES6 already adds a ton of new constructs that won't run on older browsers. Relying on symbols to be interned also won't work on older machines. A symbol-literal syntax like #symbol and #"spaced symbol" would make them far easier to use and increase their adoption. Who really wants to write map.add(new Symbol("foo"), 'bar') instead of map.add(#foo, 'bar')?
- ajuc 11y ago> Who really wants to write map.add(new Symbol("foo"), 'bar') instead of map.add(#foo, 'bar')? It makes it clear when you are generating new symbol, and when you are reusing existing symbol. var a = new Symbol("foo"); var b = new Symbol("foo"); map[a]="firstValue"; map[b]="secondValue"; Console.log(map[a] + " " + map[b]); // writes "firstValue secondValue" How does it work with your syntax? map[#foo]="firstValue"; map[#foo]="secondValue"; Console.log(map[#foo]); // what is written here?
- deleted 11y ago[deleted]
- masklinn 11y ago> Who really wants to write map.add(new Symbol("foo"), 'bar') Nobody because it's completely daft, and absolutely not equivalent to the second version, the equivalent to the second version is: map.add("foo", "bar")
- serve_yay 11y agoWhy add syntax if you don't need to?