So the issue of Array index safety came up on the discussion for the .indexed()
proposal. Now of course there's nothing wrong with arrays using integers as
indices as such, but it does mean that indices can be manipulated outside of
the array, which somewhat defeats the idea of the indexing model requiring them
to be passed back to the parent collection for manipulation.
In a few types of my own I've avoided this problem by doing the following:
public struct MyMaskedIndex : Comparable { fileprivate var raw:Int }
// Comparable conformance omitted
public struct MyType : Collection {
// Lots of stuff omitted
public func distance(from start:MyMaskedIndex, to end:MyMaskedIndex) -> Int
{
return end.raw - start.raw;
}
}
In essence MaskedIndex is still just an Int, and should optimise as such, but
in development the use of a type like this ensures that indices from my
collection can't be manipulated externally, which enables the type-checker to
conveniently prevent any mistakes I might make. It's a handy pattern for other
things like hashed values, ensuring I can't use unhashed values of the same
type by accident and so-on.
I just wanted to raise the topic to see what other people thought about the
idea of doing something similar for Array and any other types that use integer
types directly as indices? For convenience it is still possible to have a
public initialiser on the masking type(s), so that custom values can be used,
but by using MaskedIndex(raw:) it should be much more obvious what's happening.
Feedback welcome!
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution