FROM : Graham Cox
DATE : Fri Aug 29 02:12:30 2008
On 29 Aug 2008, at 4:28 am, Ken Thomases wrote:
> On Aug 27, 2008, at 5:59 PM, R.L. Grigg wrote:
>
>> Hmm, I guess the wrinkle in this particular case is if the
>> "contract" doesnt specify something that the programmer assumes to
>> be safe to do (like enumerating backwards), how can you know how to
>> implement your end?
>
> Enumerating NSArrays backwards is safe and supported by the
> framework. The earlier discussion had to do with mutating a
> collection while enumerating, which is explicitly stated to be
> unsafe. Somebody (I forget who) claimed that mutating was "safe"
> while enumerating backwards, but they were wrong. In this case,
> their wrongness was not based on things left unsaid in the contract,
> but on violating things explicitly laid out in the contract.
Well, that was me. But I do see the error of my ways... I guess it was
Michael Ash's comment that NSArray *could* change its storage half way
through enumeration (if the collection were mutated) that woke me up.
I suspect it would only do this if the collection dramatically changed
in size but clearly that wouldn't be something to rely on. I checked
my own code to make sure I wasn't following my own "advice" anywhere
and turns out I've never done it without making a copy anyway.
cheers, Graham
DATE : Fri Aug 29 02:12:30 2008
On 29 Aug 2008, at 4:28 am, Ken Thomases wrote:
> On Aug 27, 2008, at 5:59 PM, R.L. Grigg wrote:
>
>> Hmm, I guess the wrinkle in this particular case is if the
>> "contract" doesnt specify something that the programmer assumes to
>> be safe to do (like enumerating backwards), how can you know how to
>> implement your end?
>
> Enumerating NSArrays backwards is safe and supported by the
> framework. The earlier discussion had to do with mutating a
> collection while enumerating, which is explicitly stated to be
> unsafe. Somebody (I forget who) claimed that mutating was "safe"
> while enumerating backwards, but they were wrong. In this case,
> their wrongness was not based on things left unsaid in the contract,
> but on violating things explicitly laid out in the contract.
Well, that was me. But I do see the error of my ways... I guess it was
Michael Ash's comment that NSArray *could* change its storage half way
through enumeration (if the collection were mutated) that woke me up.
I suspect it would only do this if the collection dramatically changed
in size but clearly that wouldn't be something to rely on. I checked
my own code to make sure I wasn't following my own "advice" anywhere
and turns out I've never done it without making a copy anyway.
cheers, Graham
| Related mails | Author | Date |
|---|---|---|
| Adil Saleem | Aug 22, 08:29 | |
| Michael Dautermann | Aug 22, 09:10 | |
| Andrew Merenbach | Aug 22, 09:22 | |
| Graham Cox | Aug 22, 09:28 | |
| Phil | Aug 22, 11:10 | |
| Phil | Aug 22, 11:27 | |
| Graham Cox | Aug 22, 12:23 | |
| Phil | Aug 22, 14:15 | |
| Adam R. Maxwell | Aug 22, 16:48 | |
| Michael Ash | Aug 22, 17:23 | |
| Andrew Merenbach | Aug 22, 17:39 | |
| Sherm Pendley | Aug 22, 18:27 | |
| Charles Srstka | Aug 22, 23:21 | |
| Thomas Engelmeier | Aug 23, 01:24 | |
| Andrew Merenbach | Aug 25, 06:21 | |
| R.L. Grigg | Aug 27, 20:50 | |
| Ken Thomases | Aug 27, 21:19 | |
| Charles Srstka | Aug 27, 23:05 | |
| R.L. Grigg | Aug 28, 00:59 | |
| Phil | Aug 28, 03:44 | |
| R.L. Grigg | Aug 28, 05:44 | |
| Ingvar Nedrebo | Aug 28, 17:25 | |
| Michael Ash | Aug 28, 17:52 | |
| Ken Thomases | Aug 28, 20:28 | |
| Graham Cox | Aug 29, 02:12 | |
| Gerriet M. Denkman… | Aug 29, 03:50 | |
| Uli Kusterer | Aug 29, 10:55 | |
| Michael Ash | Aug 29, 18:13 |






Cocoa mail archive

