Mostly d, but partly f. It's d in that it's well-known that you can send any message you want to an id, but f in that the scenario you came up with is a slightly unusual one.
Dave
On Jun 2, 2010, at 9:57 AM, Matt Neuburg wrote:
> Here's something I stumbled on by accident. Consider the following:
>
> // MyClass.h
> #import <Foundation/Foundation.h>
> @interface MyClass : NSObject {
> }
> @end
>
> // MyClass.m
> #import "MyClass.h"
> @implementation MyClass
> - (void) testing {
> NSLog(@"testing");
> }
> @end
>
> // UnadoptedProtocolAppDelegate.h
> #import <Cocoa/Cocoa.h>
> ... // skipping irrelevant stuff
> @protocol Unadopted
> - (void) testing;
> @end
>
> // UnadoptedProtocolAppDelegate.m
> #import "UnadoptedProtocolAppDelegate.h"
> #import "MyClass.h"
> @implementation UnadoptedProtocolAppDelegate
> - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
> MyClass* mc = [[MyClass alloc] init];
> [(id)mc testing];
> }
> @end
>
> This compiles and runs fine, even though MyClass never adopted the protocol
> Unadopted. It take it that by casting mc to an id, I cause the compiler to
> grasp at the only signature for "testing" that it knows about, namely the
> one in the protocol. So it happily uses that signature without complaint,
> and at runtime the correct message is sent to the MyClass instance.
>
> So this appears to be a technique for implementing a highly informal
> protocol. (The technique is: define a protocol, don't bother adopting it
> anywhere, but send messages defined in that protocol to an id.) My question
> is, is this technique:
>
> (a) pointless and lazy
>
> (b) sneaky and clever
>
> (c) just a mistake all round
>
> (d) well known; you only just noticed this??
>
> (e) all of the above
>
> (f) none of the above
>
> :) m.
>
> --
> matt neuburg, phd = [email protected], <http://www.tidbits.com/matt/>
> A fool + a tool + an autorelease pool = cool!
> AppleScript: the Definitive Guide - Second Edition!
> http://www.tidbits.com/matt/default.html#applescriptthings
>
>
>
> _______________________________________________
>
> Cocoa-dev mailing list ([email protected])
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
> http://lists.apple.com/mailman/options/cocoa-dev/davedelong%40me.com
>
> This email sent to [email protected]
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________ Cocoa-dev mailing list ([email protected]) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [email protected]
