FROM : Graham Cox
DATE : Tue Oct 14 12:13:45 2008
On 14 Oct 2008, at 8:58 pm, Chris Suter wrote:
> The proper way to solve this, in my opinion, is to allow a way of
> specifying that the return type varies depending on the class it's
> implemented on i.e. define the alloc method in such a way so that the
> compiler knows that -[<class> alloc] returns an object of type
> <class>. Something similar could be used for [NSString string] and
> [NSMutableString string] which have a similar problem (they return an
> id, rather than an NSString or NSMutableString).
Isn't the problem here with the init method, rather than alloc?
NSString et. al. return id because they are class clusters (and
because it's the established convention), but for your own classes,
you could define your init... method to return the specific object
type. If your class is not intended for subclassing or public
consumption, that should be fine.
Having said that though, there's still the problem of the return type
not being an intrinsic part of the method signature when parsed by the
compiler, so it might not fix the problem anyway.
Best solution is just not to make the methods look the same.
--Graham
DATE : Tue Oct 14 12:13:45 2008
On 14 Oct 2008, at 8:58 pm, Chris Suter wrote:
> The proper way to solve this, in my opinion, is to allow a way of
> specifying that the return type varies depending on the class it's
> implemented on i.e. define the alloc method in such a way so that the
> compiler knows that -[<class> alloc] returns an object of type
> <class>. Something similar could be used for [NSString string] and
> [NSMutableString string] which have a similar problem (they return an
> id, rather than an NSString or NSMutableString).
Isn't the problem here with the init method, rather than alloc?
NSString et. al. return id because they are class clusters (and
because it's the established convention), but for your own classes,
you could define your init... method to return the specific object
type. If your class is not intended for subclassing or public
consumption, that should be fine.
Having said that though, there's still the problem of the return type
not being an intrinsic part of the method signature when parsed by the
compiler, so it might not fix the problem anyway.
Best solution is just not to make the methods look the same.
--Graham






Cocoa mail archive

