Skip navigation.
 
mlRe: [Foo new] vs [[Foo alloc] init]:
FROM : Gregory Weston
DATE : Sun Feb 17 23:28:49 2008

On Feb 17, 2008, at 3:06 PM, Bill Bumgarner wrote:

> On Feb 17, 2008, at 11:15 AM, Gregory Weston wrote:

>> See what you're saying? +alloc *or* +allocWithZone:.
>>
>> See the subject? +alloc. It does not say anything about 
>> +allocWithZone:.
>>
>> See this code?
>>
>> @interface MyTest : NSObject
>> @end
>>
>> @implementation MyTest
>> + (id)alloc
>> {
>>  NSLog(@"allocating");
>>  return [super alloc];
>> }
>> @end

>
> You would have to override +allocWithZone: to have any kind of 
> reliable behavior.


Or you could just not assume that something which hasn't been 
explicitly stated in the docs is guaranteed. That's what I've been 
trying to say: The only promise the documentation makes about new, 
alloc and allocWithZone is that they're peers. It doesn't say a peep 
about any of them invoking any of the others to achieve that 
equivalence. That lack of explicit promise - the utter inability to 
make such a promise in general - is why it's wrong to say without a 
whole lot of caveats that one technique is precisely equivalent to 
another. Looking at the implementation of a method, rather than the 
interface, as it exists today and saying "you can count on this" is 
an outright rejection of one of the core concepts of OO.

> +alloc is a cover for +allocWithZone: with a NULL zone.  Not that 
> you would know that from the documentation, unfortunately.


Which means, like the current behavior of new, that's an 
implementation detail that shouldn't really be relied on.

> Without assuming anything, you would have to override both 
> allocator methods;  +alloc and +allocWithZone:.


So what you're saying is that [Foo new] isn't actually reliably 
equivalent to [[Foo alloc] init]? That all we can really assume about 
the default implementation of +new is that it performs behavior 
functionally equivalent to that performed by the default +alloc (and 
then invokes init)?

Related mailsAuthorDate
mlRe: [Foo new] vs [[Foo alloc] init]: Gregory Weston Feb 15, 22:55
mlRe: [Foo new] vs [[Foo alloc] init]: Adam P Jenkins Feb 16, 00:05
mlRe: [Foo new] vs [[Foo alloc] init]: Bill Bumgarner Feb 16, 00:11
mlRe: [Foo new] vs [[Foo alloc] init]: Hamish Allan Feb 16, 00:26
mlRe: [Foo new] vs [[Foo alloc] init]: Bill Bumgarner Feb 16, 00:39
mlRe: [Foo new] vs [[Foo alloc] init]: Gregory Weston Feb 16, 13:56
mlRe: [Foo new] vs [[Foo alloc] init]: Bill Bumgarner Feb 16, 18:11
mlRe: [Foo new] vs [[Foo alloc] init]: Gregory Weston Feb 16, 22:16
mlRe: [Foo new] vs [[Foo alloc] init]: Bill Bumgarner Feb 17, 00:28
mlRe: [Foo new] vs [[Foo alloc] init]: Gregory Weston Feb 17, 14:25
mlRe: [Foo new] vs [[Foo alloc] init]: Bill Bumgarner Feb 17, 17:13
mlRe: [Foo new] vs [[Foo alloc] init]: Gregory Weston Feb 17, 20:15
mlRe: [Foo new] vs [[Foo alloc] init]: Bill Bumgarner Feb 17, 21:06
mlRe: [Foo new] vs [[Foo alloc] init]: Gregory Weston Feb 17, 23:28
mlRe: [Foo new] vs [[Foo alloc] init]: Bill Bumgarner Feb 18, 00:05
mlRe: [Foo new] vs [[Foo alloc] init]: Hamish Allan Feb 18, 00:47
mlRe: [Foo new] vs [[Foo alloc] init]: j o a r Feb 18, 01:47
mlRe: [Foo new] vs [[Foo alloc] init]: Gregory Weston Feb 18, 13:53
mlRe: [Foo new] vs [[Foo alloc] init]: Gregory Weston Feb 18, 14:11
mlRe: [Foo new] vs [[Foo alloc] init]: Jeff Johnson Feb 18, 14:46
mlRe: [Foo new] vs [[Foo alloc] init]: Alastair Houghton Feb 18, 14:58
ml[Moderator] Re: [Foo new] vs [[Foo alloc] init]: Scott Anguish Feb 18, 20:33