"Christopher Armstrong" <radix / twistedmatrix.com> wrote:

> Hi all. I'm playing around with Ruby because I like experimenting with
> concurrency systems, and found out it has continuations.
>
> I'm going to be calling user-defined code inside of a callcc block,
> and the user-defined code will either return a value or raise an
> error, and I want that value or error to be passed on to the
> continuation.
>
> I can't find any way to throw into the continuation. If there is no
> way, can I request that a #throw method be added, or something like
> that?
>

You're more likely to get a response if you can provide some kind of
starting point (even if its pseudo-code) for questions like this.

Here is an example of one inappropriate guess for your case.
It was difficult to make it this complex because (and I _am_ being
serious), you start with some code and the complicated stuff
often reduces right down.
To others:  I know, I can reduce this myself.  Resist :-)


daz



class UserError < StandardError
  def initialize(err)
    @err = err
  end
end

def user_stuff
  rn = rand(15)
  if rn > 10
    puts "* Raising error #{rn} *"
    throw(:event, UserError.new(rn))
  else
    puts "* Returning value #{rn} *"
  end
  rn
end

10.times do
  cc_ret = callcc do |cont|
    rc = catch(:event) do
      rc = user_stuff
      puts '- no error -'
      rc
    end
    rc
  end
  puts "cc: #{cc_ret.inspect}"; puts
end


### output ###

* Returning value 6 *
- no error -
cc: 6

* Returning value 8 *
- no error -
cc: 8

* Raising error 12 *
cc: #<UserError: UserError>

* Returning value 0 *
- no error -
cc: 0

* Returning value 2 *
- no error -
cc: 2

* Returning value 5 *
- no error -
cc: 5

* Returning value 10 *
- no error -
cc: 10

* Raising error 12 *
cc: #<UserError: UserError>

* Raising error 13 *
cc: #<UserError: UserError>

* Returning value 6 *
- no error -
cc: 6

##############