Re: Why the need for the id type?
Re: Why the need for the id type?
- Subject: Re: Why the need for the id type?
- From: Wagner Truppel <email@hidden>
- Date: Tue, 19 Jun 2007 01:18:30 +0200
On 19 Jun 2007, at 00:16, Greg Titus wrote:
On Jun 18, 2007, at 3:06 PM, Wagner Truppel wrote:
What if I have a method like -objectEnumerator on NSArray, NSSet,
NSDictionary, which don't share a common root class other than
NSObject, which does not implement that method. In Java, you have
nothing to cast to. How would you write generic code that can
enumerate over anything enumerable?
Java interfaces solve that problem, no?
Yes, or Objective-C protocols. But you are still casting to an
interface. You are still forced to rebuild your class hierarchy any
time this comes up (adding "implements Enumerable" to all your
classes). You are essentially adding an implementation-less
additional superclass. That adds complexity.
But let's use a more complicated example, then. How would you
implement the NSUndoManager -prepareWithInvocationTarget: method
without an "id" type to return? In this case there is no possible
interface because there is no previously specified set of valid
method names. You can really and truly call _any_ method on the
return value. Including methods that never existed when
NSUndoManager was compiled.
(Feel free to look up the NSUndoManager documentation and ask
questions if you don't understand why this is so.)
- Greg
As it's very obvious, I'm quite new to Obj-C and Cocoa. I've read
about the undo manager in Apple's main Cocoa pdf (and others), but
I'm not yet familiar with its details. It seems to me, though, that
the fictitious language I was talking about in a previous message
would solve both problems you mentioned above.
I think my question boils down to this: I'm trying to understand why
Obj-C was designed the way it was. It may be simply a matter of
historical accident. At the time Obj-C was created, java wasn't
around. Had it been, perhaps Obj-C would have been designed
differently, without id. That's the essence of my question, that is,
is id really a necessary entity on its own or could things be done
with a root class instead. My impression is that they could.
Wagner
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden