• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: [Foo new] vs [[Foo alloc] init]:
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Foo new] vs [[Foo alloc] init]:


  • Subject: Re: [Foo new] vs [[Foo alloc] init]:
  • From: Gregory Weston <email@hidden>
  • Date: Sat, 16 Feb 2008 08:31:37 -0500

Adam P Jenkins wrote:

As far as I understand, [Foo new] is exactly equivalent to [[Foo
alloc] init]

Correct.

Incorrect. Based on the documentation new (by default) does setup work analogous to alloc and then invokes init before returning the object. It does not actually use the alloc method, though, which means it's entirely possible for new to not be equivalent to an alloc/init pair.

The only ways it could be different are:

1. If +new does not in fact call +alloc, but only does something
equivalent, then if a subclass overrode the +alloc method but didn't
also override +new, then it would behave differently.  Do any classes
actually override +alloc?  If so then that might be a good reason not
to use +new.

1. As I noted above, new is documented to perform setup *equivalent to* alloc. It is not, in any place I've found, documented to invoke alloc. It is certainly not required to in 3rd-party code. Therefore assuming that it *does* call alloc is an error. It remains an error even if one can identify specific circumstances under which it's true.


2. Whether or not any classes *do* override alloc is irrelevant. What's relevant is that they *can* so assuming that they don't is an error.

Within the last couple of days, someone on comp.sys.mac.programmer.misc asked about a problem they had with some code that was written several years ago and had since been recompiled successfully on a number of different hardware and software configurations but failed on OS X. His problem turned out to be an assumption that two globals that happened to be declared sequentially would be adjacent and in the same order as memory. I don't imagine there's anyone on this list who would think that was a reasonable assumption, but it's one that he made and that worked for him for years, such that when he hit a situation where it didn't work he started looking in the wrong place for an explanation. An explanation that wouldn't have even been needed if the assumption hadn't been made in the first place. This assumption - that new is always going to be equivalent to alloc/init - is not really any more well-founded.

I'll turn the question around, though: What's a good reason *to* use new? It saves 8 keystrokes over an alloc/init pair each time, but does that really gain you anything? What portion of a typical source base is going to consist of alloc/init pairs as opposed to code that doesn't involve allocations, that invokes a more complex initializer or that uses the autoreleased factory methods? Will you write +newFooWith... methods to wrap every -initWith... method or will the default initializer be exceptional? If you do write those wrappers, will you still have saved keystrokes?
_______________________________________________


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


  • Follow-Ups:
    • Re: [Foo new] vs [[Foo alloc] init]:
      • From: "Hamish Allan" <email@hidden>
  • Prev by Date: Re: acceptable method for finding path to /usr/bin
  • Next by Date: Re: Resize box not letting clicks through
  • Previous by thread: Re: [Foo new] vs [[Foo alloc] init]:
  • Next by thread: Re: [Foo new] vs [[Foo alloc] init]:
  • Index(es):
    • Date
    • Thread