Hacker News new | ask | show | jobs
by pskocik 3675 days ago
Nothing against gotos, but the cringe from that implementation was way too strong. Why does this even get any attention at all?
3 comments

it's just a moderately clever way to do a moderately ugly thing. When I saw the title of the post, I thought "uh, how on earth would you do that?"

I didn't stop and try to reason it out on my own, but then I clicked and saw the answer, and said "heh. cute." So there you go- enough people felt the same way I did, that the voting system moved this to the 4th link on the top page. /shrug. That's the internet for you!

The author is already quite aware of the cringe factor. Read the article to the end where he writes:

"Prepare to cringe."

It's a dirty hack, this is Hacker News.

Do you have or know of a better implementation?
How about a while true loop with cases in it on a state variable.

  goto=42
  while true ; do
    case $goto in
    1 )
      code for label 1
      goes here
      break     # finish
      ;;
    42 )
      code for label 42
      goes here
      goto=1
      ;;
    esac
  done
You don't have the syntactic sugar here, and there are some inconveniences, but think of the positives: this does not claim a new stack frame for each jump, let a lone a new stack frame holding a big string variable that holds most of the script source code.
I recall seeing a coroutine library for C that did exactly this using preprocessor magic.

EDIT: /me sighs, this isn't the one I was looking for but it'll do for now. http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html / http://www.chiark.greenend.org.uk/~sgtatham/coroutines.h