[Ironruby-core] Delegates (agian)

Tomas Matousek Tomas.Matousek at microsoft.com
Thu Oct 25 17:31:12 EDT 2007

In general, any object that is somehow callable is a "function pointer" in DLR (this includes CLR delegates, CLR events, Ruby proc/lambda/Method, Python functions, JScript functions, etc.). All of them should just work in IronRuby.

The main methods that would IronRuby see on a CLR are add, remove and call. Others are just syntactic sugar and aliases (<< is alias for add). << is as right for events as it is for Ruby Array IMO (there is also no method >> on array, it's Array#delete). += -= are a little bit hacky from Ruby's point of view IMO. They are more discoverable for a CLR guys though. So it might be good to support them as well.

I don't think we need RubyDelegate, you can write

x << method(:y)
x << C.instance_method(:y).bind(obj)


x += method(:y)
x += C.instance_method(:y).bind(obj)
x -= method(:y)

if you will.

Blocks/procs should also work:

x { puts 'on click' }
x << lambda { puts 'on click' }


-----Original Message-----
From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Dermot Hogan
Sent: Thursday, October 25, 2007 1:39 PM
To: ironruby-core at rubyforge.org
Subject: [Ironruby-core] Delegates (agian)

I've been thinking about delegates and IronRuby.

After playing around with trying to work with a delegate in Ruby (procs,
lambdas and god-knows-what) I've come to the conclusion that delegates
have to be supported in a more fundamental way than trying to make a
block work as a delegate.

First off, I think IR has to support adding and removing delegates via
'+=' and '-='. That's how every other .NET language does it (IronPython
included). It doesn't seem right to use '<<' because there isn't an
equivalent '>>' (though I suppose you could define one). Anyway, you
have to be able to remove a delegate as well as add one.

Secondly, if you look at the way C# and VB address delegates, they use
syntactic kludges to get round the fact that delegates are function
pointers and are not to be invoked directly. C# uses the method name
with no parentheses and VB uses AddressOf. There isn't really an
equivalent of a function pointer in Ruby. The most natural way of doing
this, I guess is to use a symbol:

x += EventHandler.new (:y)

However, this doesn't seem right because a) it won't run and b) a symbol
isn't a function pointer. So possibly a better way might be to create a
new class 'RubyDelegate' (deriving from Delegate/MulticastDelegate)
which takes the object and method name (or symbol) and returns a real
.NET CLR delegate:

x += EventHandler(RubyDelegate.new (self, "y"))


x += RubyDelegate.new (self, "y")

Third: I notice that all the other languages (C# and VB at any rate, I
can't speak for things like F#) futz around with special delegate
keywords to help as well as fooling around with the invokation syntax.

Fourth: Delegates are plumbed into .NET at a pretty deep level. Most
people, most of the time, don't see them. But they are there - and I've
used them quite a bit in communication systems. It seems to me that you
have to recogize them in IronRuby at a similarly primitive level and not
try to pretend that a delegate is equivalent to a block.

A delegate is a function pointer. A block isn't.

Posted via http://www.ruby-forum.com/.
Ironruby-core mailing list
Ironruby-core at rubyforge.org

More information about the Ironruby-core mailing list