[swift-evolution] Add ability to validate collection indices
Alexey Komnin
interfere.work at gmail.com
Tue Dec 20 02:26:40 CST 2016
> Because I don't see any way to use it in correct code. At least, none
> that can't be more-usefully and reliably provided by returning more
> index information from mutating methods.
Ok. I agree. All the indices must be invalidated if the collection
object is mutated. I just thought about making it straight and obvious
how to check if index is still valid for simple collections like
Array. Returning additional index information from mutating methods is
a subtle thing, because some methods do not change the object layout,
for example append does not change validity of indices; other methods
may invalidate a lot or, even, all indices, for example,
replaceSubrange.
I just want to understand the purpose of indices and what behavior is
intended for them. It's unclear for me now. I just see a bunch of ways
to shoot myself in the foot and looking for a way to change that
disposition.
As I see it, the problem is: there is no way to find if index is valid
for collection. Obvious way of checking if index is within the
(startIndex..<endIndex) bounds does not work. So, I know only three
possible solutions to the problem:
1. provide a way to check if index is valid;
2. state in documentation that all indices of any collection become
invalid after each mutation;
3. state in documentation that accessing elements of collection via
index after mutation is unspecified behavior.
Am I right? Did I miss something?
I would prefer option #1, if there are no other feasible solutions to
the problem.
Thanks.
- Alex Komnin
P.S. I just noticed that our discussion fell out of swift-evolution
list. Added it back.
On Mon, Dec 19, 2016 at 8:30 PM, Dave Abrahams <dabrahams at apple.com> wrote:
>
> on Mon Dec 19 2016, Alexey Komnin <interfere.work-AT-gmail.com> wrote:
>
>>> I don't personally agree with #3, FWIW
>> Why? I thought it may be useful.
>
> Because I don't see any way to use it in correct code. At least, none
> that can't be more-usefully and reliably provided by returning more
> index information from mutating methods.
>
>> - Alex Komnin
>>
>> On Mon, Dec 19, 2016 at 4:20 PM, Dave Abrahams <dabrahams at apple.com> wrote:
>>> I don't personally agree with #3, FWIW
>>>
>>>
>>>
>>>> On Dec 19, 2016, at 3:01 AM, Alexey Komnin <interfere.work at gmail.com> wrote:
>>>>
>>>> Ok. Through the discussion I've come to the following:
>>>>
>>>> 1. collection invalidates all indices on any mutation as a common solution;
>>>> 2. collection may keep some indices valid if it is feasible;
>>>> 3. there are should be a way to check if index is valid;
>>>> 4. mutating methods should return index of inserted/replaced element.
>>>>
>>>> Did I miss anything?
>>>>
>>>> - Alex Komnin
>>>>
>>>> On Mon, Dec 19, 2016 at 6:10 AM, Dave Abrahams via swift-evolution
>>>> <swift-evolution at swift.org> wrote:
>>>>>
>>>>>> on Fri Dec 16 2016, Daniel Vollmer <swift-evolution at swift.org> wrote:
>>>>>>
>>>>>> Hi,
>>>>>>
>>>>>>> On 16 Dec 2016, at 14:56, Alexey Komnin via swift-evolution <swift-evolution at swift.org> wrote:
>>>>>>
>>>>>> [snip]
>>>>>>
>>>>>>> What do you think? I feel, like we should discuss it before I
>>>>>>> formalize it as a proposal.
>>>>>>
>>>>>> I think this is a fruitless attempt, as even if the indices are still valid,
>>>>>> they may not refer to the same elements they referenced before the mutation.
>>>>>>
>>>>>> Of course, mutating methods should state whether the invalidate existing
>>>>>> indices, but I think that’s about the best that can be reasonably
>>>>>> done.
>>>>>
>>>>> We can do a bit more. For example, RangeReplaceableCollection's
>>>>> replaceRange should return the range in the new collection state
>>>>> corresponding to the elements that were replaced.
>>>>>
>>>>> --
>>>>> -Dave
>>>>>
>>>>> _______________________________________________
>>>>> swift-evolution mailing list
>>>>> swift-evolution at swift.org
>>>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>
> --
> -Dave
More information about the swift-evolution
mailing list