swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Forwarding when subclassing is not possible...


From: Benedikt Stefansson
Subject: Forwarding when subclassing is not possible...
Date: Thu, 14 Aug 1997 22:06:23 -0700

Hi,

I've been wanting to implement this for a long time, but now I found a
pressing reason. I'd like to subclass from List, but according to
Roger's documentation that is not yet allowed. But he hints at the fact
that forwarding might solve the problem:

Verbatim from the collections subclassing reference:

"New methods are being developed to simplify subclassing from such
implementations. In the meantime, if you need to use to a collection
within the implementation of your own class, just put an instance
variable in your class and put the collection in that, and pass through
the messages of the collection you want to have available on your class
to this variable."

Now, in NeXT's manual for Objective-C they have this nifty solution for
forwarding. According to the manual, when the NeXT runtime finds that a
class can't respond to a method it always calls it one more time with
the forwardInvocation: method. If you override this method (which by
default replies by repeating the fact that it doesn't recognize the
request by the caller) you can catch and forward the method call. The
suggested implementation is to stick a method in your class which says:

- (void)forwardInvocation:(NSInvocation *)anInvocation {
    if ([someOtherObject respondsToSelector:
            [anInvocation selector]])
        [anInvocation invokeWithTarget:someOtherObject];
   else
        [self doesNotRecognizeSelector:[anInvocation selector]];
}

Here NSInvocation is an instance of a class that carries with it
information about the method and arguments that was being requested. You
choose "someOtherObject" and ask the anInvocation instance to invoke the
request with the new target.

But it appears to me that the GNU Objective-C runtime does not have this
facility.

What is the alternative? To use cut and paste to catch each and every
method in the class you want to subclass from? That is an awful number
of methods in a multilayered classstructure like collections...

Hope someone has thought about this problem before...

-Benedikt

------------------
Benedikt Stefansson                 address@hidden
Department of Economics, UCLA       Fax. (310) 825-9528
Los Angeles, CA 90095-1477          Tel. (310) 825-1777


                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.
                  ==================================


reply via email to

[Prev in Thread] Current Thread [Next in Thread]