
Finally... Goto in Javascript - aheilbut
http://www.summerofgoto.com/
======
tc
When I saw this, I was hoping for the impossible, and was predictably
disappointed. This is not the GOTO you know (at least, it will never match up
to C or to CL's TAGBODY), as it cannot jump forward to labels declared
textually later in the code.

I've worked through various ways of trying to implement goto in Javascript via
preprocessing for the parenscript CL library, and have always come away
feeling unsatisfied. You can do it, but as far as I can tell, you have to use
a trampoline.

A native goto statement in Javascript would go a long way into making JS an
acceptable compilation target.

~~~
tlrobinson
Rather than using nested while loops with labels, why not use one big while
loop and a switch statement? This seems cleaner and would allow jumping ahead,
rather than just behind.

Labels "[lbl] foo" would become "case 'foo':goto_label=false;" and gotos "goto
foo;" would become "goto_label='foo'; continue goto_loop;".

Example:

    
    
        var goto_label='top';
        goto_loop: while(goto_label) {
        switch(goto_label) {
            case 'top': goto_label=false;
            ...
            goto_label='foo'; continue goto_loop;
            ...
            case 'foo': goto_label=false;
            ...
        }}
    

Not perfect, but better.

edit: scratch that, labels within other control structures won't work, only
top level ones.

------
gruseom
A real GOTO in Javascript would be extremely welcome. Commenters bemoaning the
idea are missing the point. The value is not in writing GOTOs by hand, but in
a greatly expanded ability to add higher-level abstractions to the language.
For example, we have a lot of JS with nested functions. There's no good way to
say "return from the top-level function"; the _return_ keyword only gets you
out of the current level. If we had something like CL's _return-from_ we could
do more with JS. There are many such examples. (Edit: the while-loop hack
that's used here might be useful for this. I had thought about implementing it
with throw/catch but that seemed too heavyhanded.)

------
edw519

      Goto floor(10);
      Open window;
      Jump();

~~~
tc
I like the joke, but I consider the reflexive anti-goto attitude harmful.
Javascript would be a better language with a native goto statement. I think
people forget the context of Dijkstra's paper. Scope-local gotos are
qualitatively different than type of function-less code that Dijkstra was
referring to at the time. The clearest way to express certain algorithms
really does involve a (local) goto. And goto makes it much easier to implement
higher-level language constructs in Javascript through preprocessing.

Without goto and TCO (goto by another name), you're left to transform your
code to be run under a trampoline, which really makes the resulting JS
unreadable.

~~~
crux_
What JS really, really, really, really needs isn't TCO (well that too), but
rather call/cc.

Looking over a fairly large project here, a majority of the code is written in
CPS because of callbacks to the server or delays for user interaction.

(So far, I've avoided the need to trampoline, since my call stack isn't
getting too deep yet before it unwinds on it its own. But, then again, I
haven't done any cross-browser testing at all yet.)

~~~
frodwith
First class continuations in Javascript would be a godsend.

~~~
eru
You can get them with the Continuation Monad. (But of course that needs TOC to
work well.)

------
jsn
A funny hack. He uses "while" loops with "continue" to emulate backward jumps.
Not sure how he does forward jumps (is it implemented at all? "break" could
help, i suppose..)

Anyway, AFAICS, cross-function gotos are impossible with this implementation,
so you can't write real spaghetti code with it. Awww :).

[btw, I suppose it [edit: cross-function gotos and whatnot] should be
possible. It would probably take a full-blown continuation passing style
transform, though]

------
smikhanov
We also need ALTER operator:
<http://home.swbell.net/mck9/cobol/style/alter.html>

------
jacquesm
the fifth note is quite telling:

"NOTE: Seriously. Never use this."

------
jfoutz
For bonus points, implement COME FROM.

~~~
eru
And fork threads whenever you hit multiple COME FROMs for the same label.

------
Tichy
Javascript already has labels and break and continue. That ought to be enough
for everyone.

~~~
TallGuyShort
The problem with break and continue is that they only apply to the inner most
loop. Unless you change certain values, you're only going to reenter the same
loop immediately. In that case - goto is actually a cleaner solution.

~~~
Tichy
That's what labels are for. I think you can do

loop: for(i=0;i<10;i++){ for(...) { break loop; } }

------
stilist
Shame the site wasn’t a little more subtle about the joke, at least until the
fold.

------
edawerd
This thread was actually quite enlightening to hear the debate from both sides
on the goto statement. Love HN for that. Nevertheless, it's always healthy to
inject a little humor into the debate.

<http://xkcd.com/292/>

------
axod
It just replaces labels with named while loops, and GOTO gets replaced with
continue... So it can only GOTO things that are nested correctly, and only
jump back not forwards.

So not really a Goto in any real sense of the word.

~~~
daeken
I'm surprised that approach was taken. It's easier to rewrite the function as
a state machine, where gotos just go to the proper state.

------
TweedHeads
I would like to see just one example where a real goto is needed.

Just one...

~~~
ars
I guess you never looked?

There plenty of examples.

It's not usually _needed_ but it often helps.

------
nopassrecover
Oh dear god.

~~~
joecode
Yes?

~~~
nopassrecover
Well I assumed, as did others, that this was implemented as a joke. Gotos
aren't inherently evil but if your code is improved by using them then your
code is bad.

