News for hackers. Stuff that matters.
Re:Metaprogramming in ES6: Symbols and why they're awesome by current_call on Friday June 26, 2015 @04:58PM (#9787721)
If programming can be described as "making programs", metaprogramming could be described as "making programs making programs" - or something. You probably use metaprogramming every day perhaps without even noticing it.

Compared to other languages like Ruby or Python, JavaScript's metaprogramming features are not yet as advanced - especially when it comes to nifty tools like Operator Overloading, but ES6 is starting to level the playing field.

What isn't metaprogramming now?

This feels very relevant. http://journal.stuffwithstuff.com/2013/07/18/javascript-isnt...

Re:Metaprogramming in ES6: Symbols and why they're awesome by AdieuToLogic on Saturday June 27, 2015 @12:09AM (#9789064)
If I could up-vote recursively until the Inter-tubes segfaulted, I would on that link you provided.

A couple quotable lines from the article I have to throw out there are:

> One day, Netscape woke up from a truly epic bender to discover it had jammed a scripting language onto the web and millions of people were using it. Literally none of them liked it. Not one.

> Lots of programmers believe JavaScript is “basically” Scheme because it gives them something they want to believe: that the language they choose to use has some cachet and they don’t have to feel bad about it anymore.

Re:Metaprogramming in ES6: Symbols and why they're awesome by jstuxx on Saturday June 27, 2015 @11:48AM (#9790503)
Lots of languages claim they are Scheme or Lisp dialects (JavaScript, Python, Ruby, Lua and others) in order to boost their popularity even though they have none of Lisp's unique features but just the general features you would expect to find in any dynamically typed languages.
Re:Metaprogramming in ES6: Symbols and why they're awesome by Retra on Saturday June 27, 2015 @12:59PM (#9790740)
Python claims it's a Scheme/Lisp dialect? Where?
Re:Metaprogramming in ES6: Symbols and why they're awesome by thomasfoster96 on Friday June 26, 2015 @10:40PM (#9788881)
I think that link needs a bit of updating - JS has lexical scoping and tail call elimination now.
Re:Metaprogramming in ES6: Symbols and why they're awesome by epidemian on Friday June 26, 2015 @11:37PM (#9789005)
> JS has [...] tail call elimination now.

In a very narrow hay of "has", as in "ES2015 the spec has it, but no JS runtime implements it yet": http://kangax.github.io/compat-table/es6/#proper_tail_calls_...

I'm not trying to be a contrarian. It's just that i see lots of JS developers excited about superfluous things like class syntax or symbols, whereas real game changing additions like proper tail calls seem to pass by unnoticed, and unimplemented by JS runtimes :(

Re:Metaprogramming in ES6: Symbols and why they're awesome by current_call on Friday June 26, 2015 @11:47PM (#9789021)
I tried to create an infinite loop in Firefox and got a "InternalError: too much recursion". Then when I tried it in Chrome I got "Uncaught RangeError: Maximum call stack size exceeded".

function x() { return x(); }

Re:Metaprogramming in ES6: Symbols and why they're awesome by Kiro on Saturday June 27, 2015 @07:24AM (#9789824)
I think you misunderstood "now". It's "now" as in ES6/ES2015, not ES5 which the browsers are running by default.
Re:Metaprogramming in ES6: Symbols and why they're awesome by thomasfoster96 on Saturday June 27, 2015 @12:07AM (#9789060)
That's still expected?
Re:Metaprogramming in ES6: Symbols and why they're awesome by current_call on Saturday June 27, 2015 @12:45AM (#9789148)
It's a recursive tail call. It should run forever, like the following Scheme code.

(define (foo) (foo))

(foo)

Re:Metaprogramming in ES6: Symbols and why they're awesome by thomasfoster96 on Saturday June 27, 2015 @01:34AM (#9789231)
Ah, right, yes I get what you mean.

The main problem is still that Chrome and Firefox don't support tail call elimination. Babel (6to5) and Traceur do.

Re:Metaprogramming in ES6: Symbols and why they're awesome by strmpnk on Saturday June 27, 2015 @08:36AM (#9789955)
They don't. They support an optimization for self recursive calls but not general tail call elimination.
Re:Metaprogramming in ES6: Symbols and why they're awesome by lispm on Friday June 26, 2015 @03:42PM (#9787255)
Symbols in ES6 are much like symbols in Lisp. Basically much where Lisp software uses symbols applies to ES6.

The main difference: Lisp has simple printed representations for interned, uninterned and keyword symbols.

    CL-USER 23 > (let ((color-interned-symbols   '  (yellow   green   red))
                       (color-uninterned-symbols '(#:yellow #:green #:red))
                       (color-keyword-symbols    '( :yellow  :green  :red)))
                   (list color-interned-symbols
                         color-uninterned-symbols
                         color-keyword-symbols))
    ((YELLOW GREEN RED) (#:YELLOW #:GREEN #:RED) (:YELLOW :GREEN :RED))
Lisp also has packages for namespaces of symbols.

Here you can learn more about computing with symbols, in Lisp:

https://www.cs.cmu.edu/~dst/LispBook/

Re:Metaprogramming in ES6: Symbols and why they're awesome by roopeshv on Friday June 26, 2015 @04:35PM (#9787575)
url doesn't work (Forbidden, You do not have permission to access the requested address (URL)).

In any case, I think the book parent is referring to: http://www.amazon.com/Common-LISP-Introduction-Computation-E...

Re:Metaprogramming in ES6: Symbols and why they're awesome by lispm on Saturday June 27, 2015 @04:27AM (#9789477)
Maybe on your side. Not here. Works fine. The book is for download from there.
Re:Metaprogramming in ES6: Symbols and why they're awesome by kowdermeister on Saturday June 27, 2015 @07:49AM (#9789861)
Please, don't ever use foo/bar examples. No better way to bore and confuse the reader. It's the worst way to educate people about the possibilities of the thing you want to teach about.
Re:Metaprogramming in ES6: Symbols and why they're awesome by carussell on Saturday June 27, 2015 @11:24AM (#9790414)
In the past, I hardly ever used foo and bar in my examples, for that reason. Over the last two years or so, I've strived to start using them.

There's history of using foo/bar/baz in examples. So it's a very quick way to signal to the reader, "What this is isn't the important part. The meat of the discussion isn't in this; he crux of it lies elsewhere," and that's a pretty important thing.

There's a cost in using "real" examples. There's a cost to you the author in coming up with them, and then there's the cost to the reader who has to unpack the domain-specific concepts in the example you're synthesizing and disentangle it from what you're actually trying to demonstrate.

Do use foo and bar whenever possible.

Re:Metaprogramming in ES6: Symbols and why they're awesome by kwhitefoot on Saturday June 27, 2015 @09:14AM (#9790036)
What alternative do you have in mind?
Re:Metaprogramming in ES6: Symbols and why they're awesome by zmmmmm on Friday June 26, 2015 @07:56PM (#9788425)
The global symbol registry sounds intriguing but dangerous. Does it now provide a mechanism for client side cross domain communication? The browser spends most of its time isolating different domains, but in this instance the feature seems to be explicitly added to allow for some kind of cross domain interaction. In turn however that would seem to bring with it all the security problems of allowing it - phishing sites can frame your web site and then farm all the information out of the global symbols. Which means any use of them would have to be very careful.

Does anybody have any insight on whether my interpretation here is correct?

Re:Metaprogramming in ES6: Symbols and why they're awesome by girvo on Friday June 26, 2015 @08:28PM (#9788517)
Iirc, there are still ways to communicate between frames (postMessage rings a bell?) -- the global symbols don't really hold any information though, and are mostly just markers that are used in the code itself :)
Re:Metaprogramming in ES6: Symbols and why they're awesome by tracker1 on Friday June 26, 2015 @09:57PM (#9788758)
I'm curious if Symbols are valid for localStorage keys.. it could be used as communication channels between frames that way, I seem to remember a post for something like that, and Symbols may be a bit safer.. though not sure if Symbol.for('__my_channel__') is safer than '__my_channel__' in this case (same domain). Though the cross-domain bit is slightly interesting, I'm not really sure what the risk would be in practice.
Re:Metaprogramming in ES6: Symbols and why they're awesome by AdieuToLogic on Saturday June 27, 2015 @02:17AM (#9789296)
> the global symbols don't really hold any information though, and are mostly just markers that are used in the code itself

Global flags always represent information, be it to the people who code them up or attackers who leverage their existence.

Re:Metaprogramming in ES6: Symbols and why they're awesome by to3m on Saturday June 27, 2015 @07:55AM (#9789866)
I can't see any reliable extra information available, based on the description in the article.

The only thing I can think of offhand: time the execution of Symbol.for. Mostly, this won't be terribly illuminating, because the bulk of the work (hashing input + looking in table) will be the same in both cases, and won't take long. But, assuming the registry is a hash table, what you could do in one script is pollute the registry with enough strings to trigger several hash table rebuilds (which you can detect by outsize results from Symbol.for - you add enough strings to provide a good representative set of timings).

Then another script could detect this by doing the same operation and seeing if no call to Symbol.for took much longer than any other. This gets you 1 bit of information... is that useful?

(Also, I wonder what they do about symbol table exhaustion.)

As for the results, they don't themselves hold any extra information, I don't think. Regarding Symbol.for: if the table contained the requested symbol already, it will return that symbol. If it didn't, a new symbol will be added, and returned. These are the only two options, and the caller has no way of knowing which was taken.

And regarding Symbol.keyFor: if the input was previously returned by Symbol.for, returns true. And if not, returns false. Neither tells you anything because the caller can only have acquired the symbol by doing one of these two operations itself, meaning it isn't getting any information that it didn't in theory have already.

Re:Metaprogramming in ES6: Symbols and why they're awesome by Manishearth on Sunday June 28, 2015 @10:45AM (#9793696)
That's the thing. The "existence" of a global symbol isn't observable.

Think of `Symbol.for("foo")` as if it were the string "foo". The string "foo" is also global, when passed across iframe boundaries it still retains its meaning. One can use this string on either side. Using it doesn't observably instantiate some global flag, though internally there may be interning and whatnot. Similarly, using Symbol.for("foo") doesn't instantiate any flag, observably.

Re:Metaprogramming in ES6: Symbols and why they're awesome by Manishearth on Friday June 26, 2015 @09:13PM (#9788663)
Symbols are opaque aside from their tag. The global registry just means that you can have cross domain opaque identifiers which are equal; which is useful for communication.
Re:Metaprogramming in ES6: Symbols and why they're awesome by AdieuToLogic on Saturday June 27, 2015 @02:40AM (#9789330)
Funny... I think I've heard this line of reasoning before:

https://technet.microsoft.com/en-us/library/cc781906(v=ws.10...

Re:Metaprogramming in ES6: Symbols and why they're awesome by Manishearth on Sunday June 28, 2015 @10:43AM (#9793687)
Global symbols can be thought of as "keys which always existed" and they don't have values.

You can't determine if a global symbol is being used by another frame. They just ... exist.

Re:Metaprogramming in ES6: Symbols and why they're awesome by braythwayt on Friday June 26, 2015 @03:33PM (#9787207)

  > the only way to get the Symbols within an Object
  > is Object.getOwnPropertySymbols
Also, Reflect.getOwnKeys.
Re:Metaprogramming in ES6: Symbols and why they're awesome by seanalltogether on Friday June 26, 2015 @06:52PM (#9788216)
This is a bit confusing to me, if the key doesn't matter and no two symbols are equal, why would you ever use

var foo = Symbol('foo')

over

var foo = Symbol()

Re:Metaprogramming in ES6: Symbols and why they're awesome by kannanvijayan on Friday June 26, 2015 @06:56PM (#9788228)
Mostly as a debugging aid. Stringifying the symbol will let you get at the internal string, which can be useful for tooling, introspection, etc.
Re:Metaprogramming in ES6: Symbols and why they're awesome by ww520 on Friday June 26, 2015 @04:53PM (#9787689)
If symbols of an object cannot be iterated over, does that mean they cannot be serialized into JSON? Also a symbol doesn't have a unique string representation, Symbol('foo') != another Symbol('foo'), does that mean they cannot be deserialized from JSON?
Re:Metaprogramming in ES6: Symbols and why they're awesome by loganfsmyth on Friday June 26, 2015 @04:55PM (#9787696)
JSON is a very specific subset of JavaScript, and there is no literal syntax for Symbols, so there is no defined way to represent a symbol at the moment. Unless you wanted to encode them as a function call or something, which sounds terrible.
Re:Metaprogramming in ES6: Symbols and why they're awesome by reverius42 on Friday June 26, 2015 @09:38PM (#9788726)
I don't think they can be represented in JSON. There are already other JS primitive types that can't be represented in JSON (Date) so at least it isn't introducing any inconsistency... JSON is only a subset of JS objects.
Re:Metaprogramming in ES6: Symbols and why they're awesome by thinkingkong on Friday June 26, 2015 @05:29PM (#9787860)
Not by default. But because there are possible overlaps in keys represented as strings it probably wouldnt be too helpful. Otherwise you could write your own .toJSON method to iterate over the symbol set if you really wanted to.
Re:Metaprogramming in ES6: Symbols and why they're awesome by baddox on Friday June 26, 2015 @05:56PM (#9787991)
I don't know how the default JSON serializers/deserializers work, but I suspect that wouldn't work with symbols. You could probably get something working with Symbol.for().
Re:Metaprogramming in ES6: Symbols and why they're awesome by thomasfoster96 on Friday June 26, 2015 @10:38PM (#9788876)
I hadn't really been able see how useful well-known Symbols would be until I read this. My only thoughts now are that overriding binary operators in ES7 would be an amazing feature addition.
Re:Metaprogramming in ES6: Symbols and why they're awesome by leishulang on Friday June 26, 2015 @05:23PM (#9787831)
mirror: http://webcache.googleusercontent.com/search?q=cache:9lmo3WI...

Edit: now it's back up online.

Any examples of using symbols for variants or flyweights ?

Re:Metaprogramming in ES6: Symbols and why they're awesome by jarek-foksa on Friday June 26, 2015 @09:44PM (#9788736)
Why window.Symbol looks like a constructor, but works like a factory? Wouldn't it make more sense to have either a regular constructor (let symbol = new Symbol()) or a regular factory (let symbol = createSymbol())? If it looks like a duck, it should also walk like one.

When subclassing, what is the advantage of [Symbol.toStringTag] getter over the toString() method override? Is it just another way to do the same thing?

Re:Metaprogramming in ES6: Symbols and why they're awesome by gchpaco on Friday June 26, 2015 @10:18PM (#9788829)
The Symbol workaround cannot ever clash with uses of `with`. Granted almost everybody considers `with` to be a bad idea nowadays, but that's part of the rationale.
Re:Metaprogramming in ES6: Symbols and why they're awesome by zipolupu on Saturday June 27, 2015 @10:06AM (#9790181)
The second example of Symbol.isConcatSpreadable should assert "z" equality, not "y"
Re:Metaprogramming in ES6: Symbols and why they're awesome by nawitus on Saturday June 27, 2015 @08:08AM (#9789887)
I think "getOwnSymbols" should be "getOwnPropertySymbols".
Re:Metaprogramming in ES6: Symbols and why they're awesome by carussell on Saturday June 27, 2015 @11:33AM (#9790444)
This can be the copy editing subthread.

See zipolupu's comment https://news.ycombinator.com/item?id=9790181

The article also says:

> When you see code like rho == lho it could be converted into rho[Symbol.isAbstractEqual](lho), allowing classes to override what == means to them.

While there's not any technical error here (the examples follow proper alpha-conversion), the "l" and "r" in lho and rho stand for "left" and "right", respectively, so they should be switched.

In the Symbol.match example, "return [find];" should be... "return [index];"?

[dead]
Re:Metaprogramming in ES6: Symbols and why they're awesome by dang on Saturday June 27, 2015 @04:28AM (#9789479)
Please don't do this here.