[swift-evolution] ed/ing, InPlace, Set/SetAlgebra naming resolution

Dave Abrahams dabrahams at apple.com
Fri Feb 12 00:51:12 CST 2016


on Thu Feb 11 2016, Greg Parker <gparker-AT-apple.com> wrote:

>> On Feb 11, 2016, at 10:33 PM, David Owens II via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>>> On Feb 11, 2016, at 10:30 PM, Chris Lattner <clattner at apple.com> wrote:
>>> 
>>>> On Feb 11, 2016, at 10:28 PM, David Owens II via swift-evolution <swift-evolution at swift.org> wrote:
>
>>>> 
>>>> I don’t know if this has been suggested already… but why not just
>>>> use the same name and have an `inPlace` parameter with a default
>>>> value?
>>> 
>>> inplace, or not, fundamentally affects the operation, including the
>>> return type.  On a struct, it is the different between the method
>>> being mutating or not.
>>> 
>>> -Chris
>> 
>> The return type is not always different, but in the case where it is
>> and when we need mutating, can’t overloads solve this problem?
>> 
>> mutating func sort() {}
>> func sort(inPlace: Bool) -> T {}
>> 
>> Maybe I’m just overlooking something really obvious here…
>
> x.sort(inPlace: true) can't work with those definitions.

You need something like:

enum InPlace { case inPlace }
extension MutableCollectionType where Index : RandomAccessIndexType, Generator.Element: Comparable {
   mutating func sort(_: InPlace) {
      sortInPlace()
   }
}
var x = Array((10..<40).reverse())
x.sort(.inPlace)

-- 
-Dave


More information about the swift-evolution mailing list