resumable exceptions

  • Thread starter Ñëåïíåâ Âëàäèìèð
  • Start date
Ñ

Ñëåïíåâ Âëàäèìèð

Hi all,

maybe I'm touching an old problem here...I wrote a "Hello World"
program which uses a very simple "resumable exceptions"
infrastructure. Here we go:


class ResourceRequest < Exception
attr_accessor :key, :continuation

def initialize(key,continuation)
@key = key
@continuation = continuation
super("No resource found for key "+key.inspect)
end
end

def get_resource(key)
callcc {|cc| raise ResourceRequest.new(key,cc)}
end

def with(hash)
begin
yield
rescue ResourceRequest => e
hash.has_key?(e.key) ? e.continuation.call(hash[e.key]) : raise(e)
end
end


def print_string(str)
puts(str || get_resource("string to print"))
end

with( "string to print" => "Hello World!" ) do print_string(nil) end


If a function can't find some resource, it throws a request for it,
which is processed with scoping rules (#with calls can be nested), as
opposed to calling a global/static resource getter somewhere. We can
also play with the resource hash - for example, use class names as
keys... or use allocator methods as values, de-facto giving the block
a set of "lazily allocated" resources... or easily override the object
a specific method call interacts with, for testing purposes...

Is this interesting? =) I've been thinking about how to make functions
request missing parameters from callers; this is definitely not a
complete solution...

Vladimir Slepnev
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top