[gabriele renzi surrender_it@rc1.vip.ukl.yahoo.com, 2004-03-10 13.34 CET]
I understand hat callcc calls the block passing the continuation to
it, I did’nt meant to replace it.
I just asked why the process by witch |$cc| is initialized is not
available to us.
I just knew of scheme’s call/cc till some time ago, and I considered
that was the only way to handle continuations.
Than I found “new Continuation” in javascript, and I wondered if we
could have (on side with callcc ) something like what exist in
Rhino[1].
But your example would not work with the Rhino version.
Continuation.new creates a continuation that returns from the
enclosing block. Since the enclosing block is the top level, when you
do $cc.call, it would exit the program.
I find the ruby version clearer. When you do callcc{|c| … }, it
means: when you’ll do c.call, it will return exactly to this point.
In the Rhino version, when you do c = Continuation.new, it means: when
you’ll do c.call, it will return from the enclosing
block/function/etc…
Your modified example from Pickaxe then would become something like:
arr = [ “Freddie”, “Herbie”, “Ron”, “Max”, “Ringo” ]
dummy = begin
^-- cc.call will return here
cc = Continuation.new
end
puts(message = arr.shift)
cc.call unless message =~ /Max/
And what happens with Continuation.new inside iterators? cc.call
should return like next or like break?
def loop
while true
dummy = yield
^-(1)
end
end
dummy = loop do
^-(2)
cc = Continuation.new
break
end
puts(message = arr.shift)
cc.call unless message =~ /Max/ # ← returns to (1) (next) or (2) (break)?
Bah… it’s all more complicated… better stay with the ruby version