[swift-evolution] [Review] SE-0065 A New Model for Collections and Indices
Dmitri Gribenko
gribozavr at gmail.com
Tue Apr 12 04:11:41 CDT 2016
On Mon, Apr 11, 2016 at 9:56 PM, Brent Royal-Gordon via
swift-evolution <swift-evolution at swift.org> wrote:
> So, imagine that we have a type like this in the standard library:
>
> /// Represents a pre-validated index. A pre-validated index received from a given collection is
> /// guaranteed to refer to a valid element in that collection, as long as the collection is not mutated.
> ///
> /// -Warning: Operations which accept a Valid<Index> assume it is in bounds and do not perform
> /// bounds checks. Using a Valid<Index> on a collection other than the one which created
> /// it, or on a collection which has been mutated since it was created, may be unsafe.
> struct Valid<Index: Comparable> {
> init(unsafeIndex index: Index) { self.index = index }
> private(set) var index: Index
> }
Hi Brent,
Index invalidation rules are much more complex than what this model
can express. For example, Array's indices can become invalid after
you remove elements from it. Even if you have validated indices
around, that validation is now invalidated. Not even to mention that
indices are valid only in context of a particular collection instance,
so in this model you could validate an index against one collection
and use it with another one. Please read
https://github.com/apple/swift/blob/master/docs/IndexInvalidation.rst
for a more detailed description of the rules.
Dmitri
--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/
More information about the swift-evolution
mailing list