views:

312

answers:

3

I'm struggling to understand the concept of Continuations (as used in Seaside with Smalltalk). A snippet from Wikipedia says:

"... refer to first-class continuations, which are constructs that give a programming language the ability to save the execution state at any point and return to that point at a later point in the program..."

Isn't this simply another way of expressing what an assembler programmer would do when programming an interrupt? Or have I completely missed the point!

+1  A: 

In some ways they are similar. However, continuations are called by the program itself, and interrupts are usually generated by the CPU or devices on the computer. Also, an interrupt is more like a C signal, it is just called and then control returns to the program. The interrupt is responsible for saving state and restoring it afterwards.

Also, it should be noted that you can implement continuations quite easily in assembler.

Zifre
+1  A: 

Continuations are roughly equivalent to setjump/longjump in C. You can expect your context be intact when you call the continuation (stack, flags, registers, instruction pointer and so on) after leaving it. So it is similar to a software interrupt call except you don't have to return (or more accurately, call the continuation) after the handler finishes and the continuation is not implicit.

artificialidiot
Sounds like a GOTO in a higher language then? Are they (Continuations) good practice? I only ask because the GOTO can cause all sorts of structural problems. Thanks Kevin
KHWP
In fact, yes, they are a lot like a goto in the sense they make code harder to follow in most cases. They don't have the problem of jumping into a invalid context like imperative languages though. Unlike a label, you cannot create a continuation before you actually initialise its context.
artificialidiot
+5  A: 

A continuation can be seen as a snapshot copy of the running process. Capturing a continuation means that the current process is copied and put aside. After that, code continues to execute normally. Evaluating a continuation means that the current process is terminated and the copied one is resumed in exactly the state it was captured. Continuations can typically be resumed multiple times.

An interrupt is more like a coroutine, where there are two different execution threads (application code, interrupt handler) that interleave each other.

Lukas Renggli