Why does Ruby have callcc?

D

daz

Gavin Sinclair said:
Although you put "i = i + 1", when yyou then "goto 120", won't
the previous value of i be restored, as the continuation's tentacles reach
in to the consciousness of the program and find that moment in time?

I think variables follow the scoping rules you are used to.

#===============

IANP&T (I Am Not Penn & Teller) but this thread seems to be imbued
with notions that callcc is 'magical'.
I recommend anyone to re-read the short 'Continuations' section
in PickAxe without imagining that lots of stuff has been omitted :>


The contents of the block are not really tied to the continuation.
It's a syntactical convenience but IM(nuby+)O easy to follow.
The important things are on the edges the block:

callcc { | contname | "value of block" }
#--VV---------CC-------------------------BB-TT

where TT is the transfer point after any CC.call().
A CC.call can be made from inside or outside this block
once the Continuation has been 'defined'.

BB is a normal block <AFAICT> which can be interrupted
(or not) by CC.call() or CC.call("newBB").

The value of the callcc expression (VV) is the value
of the block (if it gets to the end), or the value
of the argument given to CC.call("newBB"), or nil if no
argument is given to CC.call.

Things to watch:
Obviously the variable used for contname (at CC) must
be in scope where you want to call it. If you use a global,
that's sorted :-b


#===============

vv = callcc { 10 }
p vv #-> 10

#===============

vv = callcc { |cc| cc.call; 10 }
p vv #-> nil

#===============

vv = callcc { |cc| cc.call(11); 10 }
p vv #-> 11

#===============

vv = callcc do |cc|
#
cc.call(13) if false
#
puts "blah, blah, stuff" #-> displays this
#
cc.call(12) if true
#
#
puts "blah, blah, loads more"
#
#
cc.call(11)
#
10
end
p vv #-> 12

#===============

The possibilities for use / abuse of this facility
are really quite large.


daz
 
T

ts

"M" == Mauricio =?iso-8859-1?Q?Fern=E1ndez?= <Mauricio> writes:

M> The closest to a rationale I've found:

[ruby-talk:69304]


Guy Decoux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,102
Messages
2,570,645
Members
47,246
Latest member
TemekaLutz

Latest Threads

Top