5

Steve Yegge mentions it in When Polymorphism Fails:

... I think this implies that type is best represented via properties rather than classes, because of the inherent inflexibility of classes. But in "traditional" languages like C++ and Java, this makes code sharing a bit harder, because of the lack of support for syntactic delegation. ...

(emphasis added)

A Google search turns up a State of the Onion address by Larry wall.

2 Answers 2

3

Assume the following:

class Foo {
    public function foo1() {...}
    public function foo2() {...}
}
class Bar {
    public function bar1() {...}
    public function bar2() {...}
}

Classically, when composing a class of these two, that delegates calls to them, it would look like:

class FooBar {
    var foo:Foo;
    var bar:Bar;
    public function foo1() { foo.foo1(); }
    public function foo2() { foo.foo2(); }
    public function bar1() { bar.bar1(); }
    public function bar2() { bar.bar2(); }        
}

There is no syntactic construct to support what you are doing. For every call you want to delegate, you will have to write the same thing over and over again.

Languages that use message passing, such as Ruby, Smalltalk and Objective-C, work around the need for that, by invoking a specific method in response to unknown messages.

It looks something like this:

class FooBar {
    var foo:Foo;
    var bar:Bar;
    override public function handleUnknownMethod(name:String, args:Array) {
         if (foo.hasMethod(name)) foo.invoke(name, args);
         else if (bar.hasMethod(name)) bar.invoke(name, args);
         else super.handleUnknowMethod(name, args);
    }
}

This is "better" in the sense that you don't have to do this for every single method. This makes composition less costly and code sharing easier.

Note that this is also not syntactic delegation. What this is, is deferring handling of calls to undefined methods to runtime, which means you can use the language itself to handle them.

Syntactic delegation would be something like:

class FooBar {
    @forwardAll var foo:Foo;
    @forwardAll var bar:Bar;      
}

That would be a syntactic sugar the compiler would resolve by generating a public method for every public method in Foo, that would merely delegate the call to foo and would do the same for bar/Bar.

2
  • Great answer! Do you know why delegates are implemented using protocols in Objective-C when the language has support for message forwarding?
    – Plumenator
    Nov 1, 2011 at 13:04
  • @Plumenator: That is a very good question and to be honest I know too little about cocoa to answer it. But I suppose if you opened a new question on the subject, you would get a good answer. ;)
    – back2dos
    Nov 1, 2011 at 13:40
1

Looks like Steve Yegge refers to explicit support of delegation by means of programming language syntax.

Given his references to C++ and Java I would say he has in mind something like support for delegation as it is provided in C#, see eg this MSDN tutorial - this tutorial refers to delegate keyword.

1
  • Cool! C# seems to have a lot of these nifty features. Events is another cool feature.
    – Plumenator
    Nov 1, 2011 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.