<div dir="ltr"><div>Good points Dimitri,</div><div><br></div><div>I only wrote this quickly, so please look for other issues too, I don&#39;t expect it to be complete by any means.</div><div><br></div><div>I think a way to get an element&#39;s hashValue should probably be exposed, similar to `<font face="monospace, monospace">collection.next(index)</font>` that I mentioned earlier. I think this is useful, but I don&#39;t think it&#39;s actually necessary. Equatable is sufficient to provide Set functionality, Hashable is just for performance</div><div><br></div><div>I&#39;m not aware of any algorithms that can&#39;t be built on top of the existing set methods that would require the same hash value the element used for binning.<br></div><div><br></div><div>I was initially thinking that you could compare the Hashable to determine equality, but I agree that there is a potential for the closure to convert to a narrower type.</div><div><br></div><div>A much stronger solution would change the interface slightly to require Element to be `<font face="monospace, monospace">Equatable`</font>, and provide `<font face="monospace, monospace">Element -&gt; Int</font>` as the closure. It&#39;s slightly less flexibly, but I do agree that a narrowing makes Equatability of s1 == s2 ambiguous.<br></div><div><br></div><div><b>In summary</b>:</div><div><br></div><div><div style="font-size:13px"><div><span style="font-family:monospace,monospace;background-color:rgb(244,204,204)">- struct Set&lt;Element: Hashable&gt; {</span><br></div><div><span style="font-family:monospace,monospace;background-color:rgb(217,234,211)">- struct Set&lt;Element: Equatable&gt; {</span></div><div><span style="font-family:monospace,monospace">    ...</span><br></div><font face="monospace, monospace" style="background-color:rgb(244,204,204)"><div><div style="font-family:arial,sans-serif;background-color:rgb(255,255,255)"><div><font face="monospace, monospace" style="background-color:rgb(244,204,204)">-   public init() { ... }</font></div><div><span style="background-color:rgb(217,234,211)"><font face="monospace, monospace">+   public init(</font></span><span style="font-family:monospace,monospace;background-color:rgb(217,234,211)">hashValue</span><span style="background-color:rgb(217,234,211)"><font face="monospace, monospace">ForElement: Element -&gt; Int) {</font></span></div><div><span style="background-color:rgb(217,234,211)"><font face="monospace, monospace">      ... </font></span></div><div><span style="background-color:rgb(217,234,211)"><font face="monospace, monospace">    }</font></span></div></div><font face="monospace, monospace" style="background-color:rgb(255,255,255)"></font></div>-   public init(minimumCapacity: Int) { ... }</font></div><div style="font-size:13px"><span style="background-color:rgb(217,234,211)"><font face="monospace, monospace">+   public init&lt;H: Hashable&gt;(minimumCapacity: Int, </font></span><span style="font-family:monospace,monospace;background-color:rgb(217,234,211)">hashValue</span><span style="font-family:monospace,monospace;background-color:rgb(217,234,211)">ForElement: Element -&gt; Int</span><span style="background-color:rgb(217,234,211)"><font face="monospace, monospace">) {</font></span></div><div style="font-size:13px"><span style="background-color:rgb(217,234,211)"><font face="monospace, monospace">      ... </font></span></div><div style="font-size:13px"><span style="background-color:rgb(217,234,211)"><font face="monospace, monospace">    }</font></span><font face="monospace, monospace" style="background-color:rgb(244,204,204)"><br></font></div><div style="font-size:13px"><div><span style="font-family:monospace,monospace">    ... (same for other public initialisers)</span></div></div><div style="font-size:13px"><span style="font-family:monospace,monospace;background-color:rgb(217,234,211)">+   public func hashValueForElement(element: Element) -&gt; Int { ... </span><span style="font-family:monospace,monospace;background-color:rgb(217,234,211)">}</span></div><div style="font-size:13px"><span style="font-family:monospace,monospace">  }</span><br></div><div style="font-size:13px"><div><br></div><div><div><span style="font-family:monospace,monospace;background-color:rgb(217,234,211)">+ extension Set where Element: Hashable {</span><span style="font-family:monospace,monospace;background-color:rgb(217,234,211)"><br></span></div><div><span style="font-family:monospace,monospace;background-color:rgb(217,234,211)">+   public init() { ... }</span><span style="font-family:monospace,monospace;background-color:rgb(217,234,211)"><br></span></div><div><span style="font-family:monospace,monospace;background-color:rgb(217,234,211)">+   public init(minimumCapacity: Int) { ... }</span><br></div><div><span style="font-family:monospace,monospace;background-color:rgb(217,234,211)">+ }</span></div></div></div></div><div><br></div><div>-</div><div>Andrew Bennett</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Feb 19, 2016 at 7:14 PM, Dmitri Gribenko <span dir="ltr">&lt;<a href="mailto:gribozavr@gmail.com" target="_blank">gribozavr@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Fri, Feb 19, 2016 at 2:06 AM, Andrew Bennett via swift-evolution<br>
&lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br>
&gt; Wrapper Solution?<br>
&gt;<br>
&gt; I&#39;ve had a go at a wrapper solution before and it seems to either need a<br>
&gt; unique type per sort, or a block stored per element (unstable).<br>
&gt;<br>
&gt; Similar overheads were discussed when an index needs to store a reference to<br>
&gt; the parent. There&#39;s some work to fix it that makes indices moveable, so<br>
&gt; instead of going index.successor() you use collection.next(index).<br>
&gt;<br>
&gt; <a href="https://github.com/apple/swift/blob/master/test/Prototypes/CollectionsMoveIndices.swift" rel="noreferrer" target="_blank">https://github.com/apple/swift/blob/master/test/Prototypes/CollectionsMoveIndices.swift</a><br>
&gt;<br>
&gt; Potential solution:<br>
&gt;<br>
&gt; The collection interfaces could change like this:<br>
&gt;<br>
&gt; - struct Set&lt;Element: Hashable&gt; {<br>
&gt; - struct Set&lt;Element&gt; {<br>
&gt;     ...<br>
&gt; -   public init() { ... }<br>
&gt; +   public init&lt;H: Hashable&gt;(elementHasher: Element -&gt; H) {<br>
&gt;       ...<br>
&gt;     }<br>
<br>
</span>How do you compare such sets?  That is, what does s1 == s2 mean, if<br>
the two sets are constructed with a hashing closure?  We can&#39;t even<br>
compare closures to find that they are the same.<br>
<br>
Also, how would this affect algorithms on sets?  When handling a set,<br>
you essentially wouldn&#39;t know the rules according to which it<br>
operates, unless we expose this mapping function.<br>
<div class="HOEnZb"><div class="h5"><br>
Dmitri<br>
<br>
--<br>
main(i,j){for(i=2;;i++){for(j=2;j&lt;i;j++){if(!(i%j)){j=0;break;}}if<br>
(j){printf(&quot;%d\n&quot;,i);}}} /*Dmitri Gribenko &lt;<a href="mailto:gribozavr@gmail.com">gribozavr@gmail.com</a>&gt;*/<br>
</div></div></blockquote></div><br></div>