These days, you hear lots of hue and cry about and how do one go about implementing them.
First question one would ask is, “What are Continuations?”
According to WikiPedia: “In computing, a continuation is a representation of the execution state of a program (for example, the call stack or values of variables) at a certain point. Many languages have constructs that allow a programmer to save the current execution state into an object, and then restore the state from this object at a later point in time (thereby resuming its execution). This technique has been used in functional programming, imperative programming, and message passing programming.”
I hope you get that, if not then fear not continuations are basically a way of returning/saving state of methods.
How?
Let me demonstrate:
def foo; return 666; end;
So, this function returns the value to the calling function, but what if we can pass where to return the call as an argument to the method:
def foo data,cont
cont rand(data)
end
I guess, that would be the gist of the matter, so in short an continuation never returns. Many people also like to see as gotos.
Whatever the thing:
Shamelessly stolen from ruby-docs here is the code that demonstrates continuations in Ruby:
arr = [ "Freddie", "Herbie", "Ron", "Max", "Ringo" ]
callcc{|$cc|}
puts(message = arr.shift)
$cc.call unless message =~ /Max/
So, what exactly is it doing? Well line #1, creates an array and second line creates a continuation object that can be passed around for calling a method. So, whenever you do call on a continuation object then the execution starts from the end of the block and hence you see loop here, even though none exists.