Re: NSObject Exercise comments?
Re: NSObject Exercise comments?
- Subject: Re: NSObject Exercise comments?
- From: Ondra Cada <email@hidden>
- Date: Tue, 13 Dec 2005 14:26:15 +0100
Andreas,
I tend to agree, but for one point:
On 13.12.2005, at 6:45, Andreas Mayer wrote:
Another weak point is, that you'd be only able to support classes
you already know about
Not quite. The theoretical byAdding method of course would *not* be
implemented this way (pseudo code):
-(Number*)byAdding:anyObject { // WRONG IMPLEMENTATION
if ([anyObject isKindOfClass:[Number class]]) return [self
byAddingNumber:anyObject];
else if ([anyObject isKindOfClass:[Matrix class]]) return [self
byAddingMatrix:anyObject];
...
}
but rather (conceptually) this way
-(Number*)byAdding:anyObjectWhichSupportsNumberValue { // proper
implementation
return [self byAddingNumber:[anyObjectWhichSupportsNumberValue
numberValue]];
}
-(Number*)numberValue {
return self;
}
That way, *any* object can either by design, or later by adding an
appropriate category, support numberValue and be thus useable.
Whether it is worth to do this (compared with publishing only the
byAddingNumber: API and letting the client code to do its own
numberValue or whatever) very much depends on the concrete class and
cannot be answered in a generic case. I would say that more often it
is better to publish the specific API (byAddingNumber:), and in a
minority of cases the generic one (byAdding:, of course accompanied
with documentation that the argument *must* respond to numberValue).
Note: generally there's no reason to check whether the argument
respondsToSelector:, for the effect would be error report -- which
the runtime does automatically if the object does not respond
anyway :) Of course there are cases when the check is needed, but
again, it's a minority of them.
---
Ondra Čada
OCSoftware: email@hidden
http://www.ocs.cz
private email@hidden
http://www.ocs.cz/oc
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden