[swift-evolution] mutating/non-mutating suggestion from a Rubyist

Dave Abrahams dabrahams at apple.com
Thu Apr 28 13:27:55 CDT 2016


on Thu Apr 28 2016, Pyry Jahkola <swift-evolution at swift.org> wrote:

> Good that you brought the topic of "fluent" interfaces up. I don't see any
> problem with explicit value type mutation and method chaining because fluent
> interfaces are constrained to reference types by the language. Details below:
>
>     On 28 Apr 2016, at 03:44, Tyler Cloutier
>     <cloutiertyler at aol.com> wrote:
>
>     How would this chain if I wanted to do something like:
>
>     let median = foo.calculateBigHugeArray().sort().medianValue()
>
>     and I want the sort to be done in place.
>
> I think I can guess what you wanted the above to mean but, mind you, the
> in-place sort returns `()` so you wouldn't chain its result like that. On the
> other hand, the above code already works using the non-mutating `.sort()` (to be
> known as `.sorted()` in Swift 3), and—correct me if I'm wrong—the compiler
> probably optimises the copy away using copy-on-write anyway.

No, the compiler can't automatically turn non-mutating operations into
in-place operations.

-- 
Dave



More information about the swift-evolution mailing list