RE: Convenience Methods
RE: Convenience Methods
- Subject: RE: Convenience Methods
- From: Jeff Laing <email@hidden>
- Date: Wed, 26 Sep 2007 12:56:22 +1000
> But class clusters are an implementation detail, and the code
> shouldn't care. What's important is +[NSMutableString
Once again, you've confused things.
He's writing "the code". Not calling it. Writing it. He has to take
implementation detail into account and thats what he was asking - how should
he implement this, return 'id' or 'myclass*'
> shouldn't care. What's important is +[NSMutableString
> stringWithString:] returns an NSMutableString, and +[NSString
> stringWithString:] returns an NSString, despite the fact that
> +stringWithString: is only defined by NSString.
Now, when I look at the online docs for NSMutableString, I see that it
doesn't actually define stringWithString: as a class method.
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/
NSMutableString_Class/Reference/Reference.html
so what you have is an indeterminate API. The docs for NSString
stringWithString: say
"stringWithString:
Returns a string created by copying the characters from another given
string."
which isn't guaranteed to be true, is it? There is no need for it to "copy"
bytes from that other string - thats the whole point of class clusters, that
they can optimise away unneeded operations. I would expect that the
implementation of [NSString stringWithString:] would look at what it was
copying and just add a reference to the data bytes, if they were
non-mutable.
And it doesn't say 'returns a mutable string' so irrespective of what
[NSMutableString stringWithString:] returns, you should not be treating it
as though it were mutable. This is where the whole 'mutable' thing falls
down. In my opinion (and thats what this is, an opinion), it is better to
explicitly declare return types rather than fall back on returning id, in
the general case, irrespective of the confusion that class clusters bring to
the issue. The fact that Apple (and presumably Next) didn't do it sensibly
is irrelevant to my opinion.
The compiler doesn't catch me doing something stupid like:
NSMutableString *s = [NSString
stringWithString:iHaveNoIdeaIfThisWasMutable];
because its completely legal. I need to wait till runtime, and many lines
later in my source, to discover that I made an error that an explicit
message prototype would have found at compile time. In terms of
productivity of programmer, there's no comparison.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden