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