[swift-evolution] Marking sort and sorted with rethrows

Tim Vermeulen tvermeulen at me.com
Sun Jun 5 13:14:30 CDT 2016


Most standard library functions that take a closure allow that closure to throw (and those functions are subsequently marked with rethrows). sort and sorted are exceptions to this. I couldn’t find this documented anywhere, but I assume this is because sorting can happen in-place and it would be impossible to restore the array to its original state without giving up performance. Correct me if I’m wrong.

I’d like to propose that we let sort rethrow anyways, and leave the array in an intermediate state (where the elements are in an arbitrary order) when an error is thrown. As long as this is properly documented, this shouldn’t lead to any confusion. Best of all, it would allow sorted to rethrow as well in which there is no room for confusion at all because it doesn’t mutate any of the user’s variables.


More information about the swift-evolution mailing list