<div dir="ltr">Exactly. While rethrows for sort make sense, since there is no user variable modification, sortInPlace either needs to revert the array (which is potentially expensive) or leave it in an arbitrary state, neither of which are good choices. I can’t seem to find any mutating function that rethrows, and I think it’s for precisely this reason.</div><br><div class="gmail_quote"><div dir="ltr">On Tue, Jun 7, 2016 at 10:20 AM Dave Abrahams &lt;<a href="mailto:dabrahams@apple.com">dabrahams@apple.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
on Mon Jun 06 2016, Saagar Jha &lt;saagarjha28-AT-gmail.com&gt; wrote:<br>
<br>
&gt; Might I add that leaving an array in an arbitrary and<br>
&gt; implementation-dependent state is also surprising to users as well as not<br>
&gt; very useful-to the user this is nothing more than a random permutation.<br>
<br>
True, but the cost of being able to restore the original ordering, when<br>
that restoration may not be needed at all, is prohibitive.  It&#39;s often<br>
the case that the caller will be throwing away the partially-modified<br>
original when an error is thrown.<br>
<br>
&gt; On Mon, Jun 6, 2016 at 5:31 PM Dave Abrahams via swift-evolution &lt;<br>
&gt; <a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br>
&gt;<br>
&gt;&gt;<br>
&gt;&gt; on Sun Jun 05 2016, Haravikk &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; &gt;&gt; On 5 Jun 2016, at 19:14, Tim Vermeulen via swift-evolution &lt;<br>
&gt;&gt; <a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Most standard library functions that take a closure allow that<br>
&gt;&gt; &gt;&gt; closure to throw (and those functions are subsequently marked with<br>
&gt;&gt; &gt;&gt; rethrows). sort and sorted are exceptions to this. I couldn’t find<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;&gt; this documented anywhere, but I assume this is because sorting can<br>
&gt;&gt; &gt;&gt; happen in-place and it would be impossible to restore the array to<br>
&gt;&gt; &gt;&gt; its original state without giving up performance. Correct me if I’m<br>
&gt;&gt; &gt;&gt; wrong.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; I’d like to propose that we let sort rethrow anyways, and leave the<br>
&gt;&gt; &gt;&gt; array in an intermediate state (where the elements are in an<br>
&gt;&gt; &gt;&gt; arbitrary order) when an error is thrown. As long as this is<br>
&gt;&gt; &gt;&gt; properly documented, this shouldn’t lead to any confusion. Best of<br>
&gt;&gt; &gt;&gt; all, it would allow sorted to rethrow as well in which there is no<br>
&gt;&gt; &gt;&gt; room for confusion at all because it doesn’t mutate any of the<br>
&gt;&gt; &gt;&gt; user’s variables.<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; This sounds reasonable; worst case with in-place sorting is that the<br>
&gt;&gt; &gt; collection was sorted in one order, and is only partially sorted in a<br>
&gt;&gt; &gt; new one, but the exception and your handling of it should be able to<br>
&gt;&gt; &gt; account for this.<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; It will require documentation to be clear that sorting methods should<br>
&gt;&gt; &gt; take care not to leave anything incomplete if a closure throws; most<br>
&gt;&gt; &gt; algorithms should be fine since they usually just test the closure<br>
&gt;&gt; &gt; then swap two values afterwards (where necessary) so there’s nothing<br>
&gt;&gt; &gt; really to interrupt, but anything that uses some kind of buffering may<br>
&gt;&gt; &gt; need to be redesigned to ensure there’s a fallback to ensure no<br>
&gt;&gt; &gt; elements are ever lost.<br>
&gt;&gt;<br>
&gt;&gt; Ensuring that no elements are ever lost is not a particularly useful<br>
&gt;&gt; goal, and not a constraint to which I would want to hold the standard<br>
&gt;&gt; library.<br>
&gt;&gt;<br>
&gt;&gt; --<br>
&gt;&gt; Dave<br>
&gt;&gt;<br>
&gt;&gt; _______________________________________________<br>
&gt;&gt; swift-evolution mailing list<br>
&gt;&gt; <a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
&gt;&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
&gt;&gt;<br>
<br>
--<br>
Dave<br>
</blockquote></div><div dir="ltr">-- <br></div><div data-smartmail="gmail_signature"><div dir="ltr">-Saagar Jha</div></div>