[swift-evolution] [RFC] New collections model: collections advance indices

plx plxswift at icloud.com
Tue Mar 8 09:27:44 CST 2016


So far I can’t think of a single index that would have difficulty implementing `Hashable`.

One policy question though:

  struct MutationTrackingTreeIndex<T> {
    private let mutation: Int
    private unsafe(unowned) let treeNode: TreeNode<T>
  }

  // which hashValue would you expect:
  extension MutationTrackingTreeIndex : Hashable {

    var hashValue: Int {
      return ObjectIdentifier(treeNode).hashValue
    }

    var hashValue: Int {
      return ObjectIdentifier(treeNode).hashValue ^ self.mutation
    }

  }

…I’d assume the 2nd, but there’s then going to be room for confusion vis-a-vis how it works with Array (and other Int-indexed collections, or even e.g. an “Array” backed by a persistent tree of some kind).

I think `Hashable` indices is a good idea but I would want the expectation for considerations like the above to be documented for consistency’s sake.

> On Mar 7, 2016, at 7:25 PM, Dmitri Gribenko via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Hi,
> 
> What does everyone think about requiring indices to conform to
> Hashable, in addition to the existing requirements for Equatable and
> Comparable?
> 
> I don't think this should limit any viable collection designs, and yet
> might be useful, for example, to store indices in a set.
> 
> 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>*/
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list