T
treefrog
Hi folks,
I've been playing around with continuations a bit. What I'd like to be
abale to do is execute arbitary code sequences, suspend them, and then
resume them. All very well, and all what I can do using Continuation.
The problem is, that I want to be able to modify certain aspects of the
context while the code is suspended, and then have these picked up when
I resume. Consider the following code:
#callcc test
class Func
attr_reader :cont
def initialize
end
def do
@cont ? cont.call : do1
puts "after cont call"
end
def do1
puts 'A'
callcc { |cc| @cont=cc; throw :wait}
puts 'B'
@cont=nil
end
end
f=Func.new
(1..2).each do |i|
puts "Before do : i= #{i}"
catch :wait do
f.do
end
puts "After do : i= #{i}"
end
Now consider the output:
Before do : i= 1
A
After do : i= 1
Before do : i= 2
B
after cont call
After do : i= 1
Before do : i= 2
A
After do : i= 2
Basically, what is happening is that the continuation is the entire
programme context, and hence the loop variable i is overwritten.
This isn't quite what I am trying to achieve, which is to be able to
use a throw / catch mechanism in conjunction with the continuation to
be able to jump out of a function at arbitary points, and then return
to it later. What I really want is to be able to keep the contxt from
the catch downwards, so that I can keep the higher layers of the
context. In this case the desired output would be:
Before do : i= 1
A
After do : i= 1
Before do : i= 2
B
after cont call
After do : i= 2
Any ideas?
Best regards
Steve
PS (before anyone asks, I'm trying to put together a library for
discrete event simulation, and I'd like to be able to easily specify
arbitary breakpoints in functions, and then resume at them later, in
the knowledge that some (higher level) data may have changed.
I've been playing around with continuations a bit. What I'd like to be
abale to do is execute arbitary code sequences, suspend them, and then
resume them. All very well, and all what I can do using Continuation.
The problem is, that I want to be able to modify certain aspects of the
context while the code is suspended, and then have these picked up when
I resume. Consider the following code:
#callcc test
class Func
attr_reader :cont
def initialize
end
def do
@cont ? cont.call : do1
puts "after cont call"
end
def do1
puts 'A'
callcc { |cc| @cont=cc; throw :wait}
puts 'B'
@cont=nil
end
end
f=Func.new
(1..2).each do |i|
puts "Before do : i= #{i}"
catch :wait do
f.do
end
puts "After do : i= #{i}"
end
Now consider the output:
Before do : i= 1
A
After do : i= 1
Before do : i= 2
B
after cont call
After do : i= 1
Before do : i= 2
A
After do : i= 2
Basically, what is happening is that the continuation is the entire
programme context, and hence the loop variable i is overwritten.
This isn't quite what I am trying to achieve, which is to be able to
use a throw / catch mechanism in conjunction with the continuation to
be able to jump out of a function at arbitary points, and then return
to it later. What I really want is to be able to keep the contxt from
the catch downwards, so that I can keep the higher layers of the
context. In this case the desired output would be:
Before do : i= 1
A
After do : i= 1
Before do : i= 2
B
after cont call
After do : i= 2
Any ideas?
Best regards
Steve
PS (before anyone asks, I'm trying to put together a library for
discrete event simulation, and I'd like to be able to easily specify
arbitary breakpoints in functions, and then resume at them later, in
the knowledge that some (higher level) data may have changed.