Re: 3 obj-c/cocoa beginner q's
Re: 3 obj-c/cocoa beginner q's
- Subject: Re: 3 obj-c/cocoa beginner q's
- From: Greg Titus <email@hidden>
- Date: Tue, 25 Mar 2003 12:55:10 -0800
On Tuesday, March 25, 2003, at 12:01 PM, Hamish Allan wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Tue Mar 25, 2003 15:12:55 Europe/London, David Remahl
<email@hidden> wrote:
And because [NSArray count] could have side effects (say, change
something that is read during the rest of the loop, or maybe even
change the data i refers to!) the compiler has no way to optimize
away that step.
Out of interest, can anyone tell me why "- (int) count const;" is not
possible?
Because in Objective-C, the actual implementation of -count that will
be called at run-time is completely dynamic. There is no way for the
compiler to determine that the const implementation is the one which
will get used, so the compiler can't optimize it out.
For an example of why this is impossible for the compiler, imagine that
I write a subclass of NSArray that contains a -count method with
side-effects. I can put that subclass in a bundle which is loaded at
run-time into some application, and all of that application's code (and
all of the frameworks it links against) which deals with NSArrays must
still work correctly.
The object that the code deals with might not even be a real NSArray,
but some sort of proxy or other class which implements methods with the
same selectors. So you wouldn't even be able to restrict the -count
implementations to all be constant by forcing that constraint on
subclasses.
Hope this helps,
- Greg
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.