views:

235

answers:

1

And I mean that in the same sense that a C/Java for is just a funky syntax for a while loop.

I still remember when first learning about the for loop in C, the mental effort that had to go into understanding the execution sequence of the three control expressions relative to the loop statement. Seems to me the same sort of effort has to be applied to understand Continuations (in Scala and I guess probably other languages).

And then there's the obvious follow-up question... if so, then what's the point? It seems like a lot of pain (language complexity, programmer errors, unreadable programs, etc) for no gain.

+3  A: 

In some sense, yes, continuations are funky syntax for using callbacks. You can manually perform a very complex global transformation on your code (the so called continuation-passing-style transformation), and you will get continuations on your hands without direct language support.

However, transforming your entire codebase is probably not very practical, and the resulting code is hard to read, so having the compiler do it for you behind the scenes is MUCH better.

hzap
Yes, but.... the Scala continuations feature does NOT do the transformation for you behind the scenes. Actually it's just the opposite, it makes you write code in crazy hard-to-read continuation-passing-style and transforms it into regular imperative-style JVM bytecode. Where's the advantage?
Alex R
http://lamp.epfl.ch/~rompf/continuations-icfp09.pdf (the scala continuations paper) talks about how it uses CPS transform to implement `shift` and `reset`. It's got a lot of gritty details, but basically the idea involves flipping all the definitions and function applications between a `shift` and a `reset` inside out. This would be pretty ugly code if written out by hand.
hzap
Alex, your comment is incorrect. The whole point of the continuations plugin is that you *don't* have to write your code in continuation-passing style. I think you are confused about what "continuation-passing style" even is.
Seth Tisue