# mutating/non-mutating suggestion from a Rubyist

**URL:** <https://forums.swift.org/t/mutating-non-mutating-suggestion-from-a-rubyist/2326>\
**Category:** Discussion\
**Created:** [April 24, 2016, 9:32am UTC](https://forums.swift.org/t/mutating-non-mutating-suggestion-from-a-rubyist/2326 "2016-04-24T09:32:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_F](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@James\_F](https://forums.swift.org/u/James_F)\
**Post date:** [April 24, 2016, 9:32am UTC](https://forums.swift.org/t/mutating-non-mutating-suggestion-from-a-rubyist/2326/1 "2016-04-24T09:32:42Z")

</div>

The whole naming issue seems to be caused by the .union(\_:) function. The Swift Guidelines say that mutating functions should use a verb, and non-mutating forms should use a noun, but in this case, the word union itself is a verb and a noun.

Have we considered this, then:

a.union(b) //mutating

\_ = a.union(b) //non-mutating

There is no ambiguity in most situations, and the fact the Swift compiler can't disambiguate this at the moment is a bug I'd like to see fixed in the Swift 3 timeframe. I think this wouldn't be such a bad compromise, and other functions could still use the standard -ed/-ing system alongside this without the API looking inconsistent, unlike with the form- prefix.

Admittedly, there is merit to the idea that functional methods should make non-mutating forms the primary form, but I feel like we should figure out what our stance is on this methodology in general. A mention in the Guidelines one way or the other would be nice, since the current rules seem to support this.

From James F

---

<div class="post-metadata">

**Author:** ![Patrick\_Smith](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/patrick_smith/32/3428_2.png) [@Patrick\_Smith](https://forums.swift.org/u/Patrick_Smith)\
**Post date:** [April 24, 2016, 12:54pm UTC](https://forums.swift.org/t/mutating-non-mutating-suggestion-from-a-rubyist/2326/2 "2016-04-24T12:54:32Z")

</div>

Yes +1 I think how the compiler can’t work with two methods with the same name, where one has a result, and other mutating, needs to be fixed to enable nice APIs.

Patrick

The whole naming issue seems to be caused by the .union(\_:) function. The Swift Guidelines say that mutating functions should use a verb, and non-mutating forms should use a noun, but in this case, the word union itself is a verb and a noun.

Have we considered this, then:

a.union(b) //mutating

\_ = a.union(b) //non-mutating

There is no ambiguity in most situations, and the fact the Swift compiler can't disambiguate this at the moment is a bug I'd like to see fixed in the Swift 3 timeframe. I think this wouldn't be such a bad compromise, and other functions could still use the standard -ed/-ing system alongside this without the API looking inconsistent, unlike with the form- prefix.

Admittedly, there is merit to the idea that functional methods should make non-mutating forms the primary form, but I feel like we should figure out what our stance is on this methodology in general. A mention in the Guidelines one way or the other would be nice, since the current rules seem to support this.

> **···**
>
> On Sun, Apr 24, 2016 at 2:33 AM -0700, "James Froggatt via swift-evolution" \<swift-evolution@swift.org\> wrote:
> 
> > From James F
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> swift-evolution mailing list  
> swift-evolution@swift.org  
> [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![timv](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/timv/32/13304_2.png) [@timv](https://forums.swift.org/u/timv)\
**Post date:** [April 24, 2016, 2:49pm UTC](https://forums.swift.org/t/mutating-non-mutating-suggestion-from-a-rubyist/2326/3 "2016-04-24T14:49:41Z")

</div>

> The whole naming issue seems to be caused by the .union(\_:) function. The Swift Guidelines say that mutating functions should use a verb, and non-mutating forms should use a noun, but in this case, the word union itself is a verb and a noun.
> 
> Have we considered this, then:
> 
> a.union(b) //mutating
> 
> \_ = a.union(b) //non-mutating
> 
> There is no ambiguity in most situations, and the fact the Swift compiler can't disambiguate this at the moment is a bug I'd like to see fixed in the Swift 3 timeframe. I think this wouldn't be such a bad compromise, and other functions could still use the standard -ed/-ing system alongside this without the API looking inconsistent, unlike with the form- prefix.
> 
> Admittedly, there is merit to the idea that functional methods should make non-mutating forms the primary form, but I feel like we should figure out what our stance is on this methodology in general. A mention in the Guidelines one way or the other would be nice, since the current rules seem to support this.
> 
> \> From James F

Can’t we do this for every mutating method? i.e.

var numbers = [1,3,2]  
let sorted = numbers.sort()  
// sorted is [1,2,3], numbers is [1,3,2]  
numbers.sort()  
// numbers is [1,2,3]

I suppose this would require that the mutating version doesn’t return anything, and I don’t know if that’s ever a problem.

---

<div class="post-metadata">

**Author:** ![James\_F](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@James\_F](https://forums.swift.org/u/James_F)\
**Post date:** [April 24, 2016, 3:38pm UTC](https://forums.swift.org/t/mutating-non-mutating-suggestion-from-a-rubyist/2326/4 "2016-04-24T15:38:34Z")

</div>

The idea of distinguishing all mutating/non-mutating functions with only the assignment operator did occur to me as I wrote that.  
Using such a rule would allow automatic generation of mutating methods from non-mutating ones, since the naming would no longer need changing.  
However, this would also mean scrapping half the Naming Guidelines, so I'm hesitant to put that possibility forward as a serious proposal.

I think union (verb) vs union (noun) would work as a one off, though, since it fits the guidelines as they currently stand. It would be a nice way to demonstrate that the compiler can make the distinction in a public API.

From James F

> **···**
>
> On 24 Apr 2016, at 15:49, Tim Vermeulen \<tvermeulen@me.com\> wrote:
> 
> > > The whole naming issue seems to be caused by the .union(\_:) function. The Swift Guidelines say that mutating functions should use a verb, and non-mutating forms should use a noun, but in this case, the word union itself is a verb and a noun.
> > > 
> > > Have we considered this, then:
> > > 
> > > a.union(b) //mutating
> > > 
> > > \_ = a.union(b) //non-mutating
> > > 
> > > There is no ambiguity in most situations, and the fact the Swift compiler can't disambiguate this at the moment is a bug I'd like to see fixed in the Swift 3 timeframe. I think this wouldn't be such a bad compromise, and other functions could still use the standard -ed/-ing system alongside this without the API looking inconsistent, unlike with the form- prefix.
> > > 
> > > Admittedly, there is merit to the idea that functional methods should make non-mutating forms the primary form, but I feel like we should figure out what our stance is on this methodology in general. A mention in the Guidelines one way or the other would be nice, since the current rules seem to support this.
> > > 
> > > > From James F
> > 
> > Can’t we do this for every mutating method? i.e.
> > 
> > var numbers = [1,3,2]  
> > let sorted = numbers.sort()  
> > // sorted is [1,2,3], numbers is [1,3,2]  
> > numbers.sort()  
> > // numbers is [1,2,3]
> > 
> > I suppose this would require that the mutating version doesn’t return anything, and I don’t know if that’s ever a problem.

---

<div class="post-metadata">

**Author:** ![VladimirS](https://avatars.discourse-cdn.com/v4/letter/v/f9ae1b/32.png) [@VladimirS](https://forums.swift.org/u/VladimirS)\
**Post date:** [April 25, 2016, 12:37pm UTC](https://forums.swift.org/t/mutating-non-mutating-suggestion-from-a-rubyist/2326/5 "2016-04-25T12:37:41Z")

</div>

Well, right now(Swift 2) we can have such code (yes, it will raise warnings, but will compile and run without errors. i.e. this is a valid code):

var a = [1,2,3]  
let a1 = a.sortInPlace(\>) // a1 == ()  
let a2 = a.sort(\>) // a2 == [3,2,1]

Note, how sortInPlace is explicit. Now, change to sort/sorted:

var a = [1,2,3]  
let a1 = a.sorted(\>) // IMO not explicit about the result  
let a2 = a.sort(\>)

For your proposal we need to disallow assignment of Void to variable, otherwise compiller can't choose which one to use. Or, in case of your proposal, documentation should explicitly say(and compiler implemented in this way) that non-mutating function should be selected if result is used.

> **···**
>
> On 24.04.2016 17:49, Tim Vermeulen via swift-evolution wrote:
> 
> > Can’t we do this for every mutating method? i.e.
> > 
> > var numbers = [1,3,2]  
> > let sorted = numbers.sort()  
> > // sorted is [1,2,3], numbers is [1,3,2]  
> > numbers.sort()  
> > // numbers is [1,2,3]
> > 
> > I suppose this would require that the mutating version doesn’t return anything, and I don’t know if that’s ever a problem.

---

<div class="post-metadata">

**Author:** ![timv](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/timv/32/13304_2.png) [@timv](https://forums.swift.org/u/timv)\
**Post date:** [April 24, 2016, 4:01pm UTC](https://forums.swift.org/t/mutating-non-mutating-suggestion-from-a-rubyist/2326/6 "2016-04-24T16:01:50Z")

</div>

> The idea of distinguishing all mutating/non-mutating functions with only the assignment operator did occur to me as I wrote that.  
> Using such a rule would allow automatic generation of mutating methods from non-mutating ones, since the naming would no longer need changing.  
> However, this would also mean scrapping half the Naming Guidelines, so I'm hesitant to put that possibility forward as a serious proposal.
> 
> I think union (verb) vs union (noun) would work as a one off, though, since it fits the guidelines as they currently stand. It would be a nice way to demonstrate that the compiler can make the distinction in a public API.
> 
> \> From James F
> 
> \> \> The whole naming issue seems to be caused by the .union(\_:) function. The Swift Guidelines say that mutating functions should use a verb, and non-mutating forms should use a noun, but in this case, the word union itself is a verb and a noun.  
> \> \>  
> \> \> Have we considered this, then:  
> \> \>  
> \> \> a.union(b) //mutating  
> \> \>  
> \> \> \_ = a.union(b) //non-mutating  
> \> \>  
> \> \> There is no ambiguity in most situations, and the fact the Swift compiler can't disambiguate this at the moment is a bug I'd like to see fixed in the Swift 3 timeframe. I think this wouldn't be such a bad compromise, and other functions could still use the standard -ed/-ing system alongside this without the API looking inconsistent, unlike with the form- prefix.  
> \> \>  
> \> \> Admittedly, there is merit to the idea that functional methods should make non-mutating forms the primary form, but I feel like we should figure out what our stance is on this methodology in general. A mention in the Guidelines one way or the other would be nice, since the current rules seem to support this.  
> \> \>  
> \> \> \> From James F  
> \>  
> \> Can’t we do this for every mutating method? i.e.  
> \>  
> \> var numbers = [1,3,2]  
> \> let sorted = numbers.sort()  
> \> // sorted is [1,2,3], numbers is [1,3,2]  
> \> numbers.sort()  
> \> // numbers is [1,2,3]  
> \>  
> \> I suppose this would require that the mutating version doesn’t return anything, and I don’t know if that’s ever a problem.

Well, this change would render a big part of the naming guidelines meaningless, but isn’t that a good thing? Guidelines are often in place to prevent ambiguity, and this solution would do that without the need for guidelines.

Anyways, I wouldn’t be surprised if this idea has come up before and has been rejected, but to me it sounds like a good idea.

> **···**
>
> > On 24 Apr 2016, at 15:49, Tim Vermeulen\<tvermeulen@me.com\>wrote:

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [April 24, 2016, 4:27pm UTC](https://forums.swift.org/t/mutating-non-mutating-suggestion-from-a-rubyist/2326/7 "2016-04-24T16:27:22Z")

</div>

Yes, I suggested this a while back, and it was rejected.

> **···**
>
> On Sun, Apr 24, 2016 at 11:01 AM, Tim Vermeulen via swift-evolution \< swift-evolution@swift.org\> wrote:
> 
> > \> The idea of distinguishing all mutating/non-mutating functions with only  
> > the assignment operator did occur to me as I wrote that.  
> > \> Using such a rule would allow automatic generation of mutating methods  
> > from non-mutating ones, since the naming would no longer need changing.  
> > \> However, this would also mean scrapping half the Naming Guidelines, so  
> > I'm hesitant to put that possibility forward as a serious proposal.  
> > \>  
> > \> I think union (verb) vs union (noun) would work as a one off, though,  
> > since it fits the guidelines as they currently stand. It would be a nice  
> > way to demonstrate that the compiler can make the distinction in a public  
> > API.  
> > \>  
> > \> \> From James F  
> > \> On 24 Apr 2016, at 15:49, Tim Vermeulen\<tvermeulen@me.com\>wrote:  
> > \>  
> > \> \> \> The whole naming issue seems to be caused by the .union(\_:)  
> > function. The Swift Guidelines say that mutating functions should use a  
> > verb, and non-mutating forms should use a noun, but in this case, the word  
> > union itself is a verb and a noun.  
> > \> \> \>  
> > \> \> \> Have we considered this, then:  
> > \> \> \>  
> > \> \> \> a.union(b) //mutating  
> > \> \> \>  
> > \> \> \> \_ = a.union(b) //non-mutating  
> > \> \> \>  
> > \> \> \> There is no ambiguity in most situations, and the fact the Swift  
> > compiler can't disambiguate this at the moment is a bug I'd like to see  
> > fixed in the Swift 3 timeframe. I think this wouldn't be such a bad  
> > compromise, and other functions could still use the standard -ed/-ing  
> > system alongside this without the API looking inconsistent, unlike with the  
> > form- prefix.  
> > \> \> \>  
> > \> \> \> Admittedly, there is merit to the idea that functional methods  
> > should make non-mutating forms the primary form, but I feel like we should  
> > figure out what our stance is on this methodology in general. A mention in  
> > the Guidelines one way or the other would be nice, since the current rules  
> > seem to support this.  
> > \> \> \>  
> > \> \> \> \> From James F  
> > \> \>  
> > \> \> Can’t we do this for every mutating method? i.e.  
> > \> \>  
> > \> \> var numbers = [1,3,2]  
> > \> \> let sorted = numbers.sort()  
> > \> \> // sorted is [1,2,3], numbers is [1,3,2]  
> > \> \> numbers.sort()  
> > \> \> // numbers is [1,2,3]  
> > \> \>  
> > \> \> I suppose this would require that the mutating version doesn’t return  
> > anything, and I don’t know if that’s ever a problem.  
> > \>  
> > \>  
> > \>
> > 
> > Well, this change would render a big part of the naming guidelines  
> > meaningless, but isn’t that a good thing? Guidelines are often in place to  
> > prevent ambiguity, and this solution would do that without the need for  
> > guidelines.
> > 
> > Anyways, I wouldn’t be surprised if this idea has come up before and has  
> > been rejected, but to me it sounds like a good idea.

---

<div class="post-metadata">

**Author:** ![dabrahams](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dabrahams/32/9815_2.png) [@dabrahams](https://forums.swift.org/u/dabrahams)\
**Post date:** [April 25, 2016, 10:00pm UTC](https://forums.swift.org/t/mutating-non-mutating-suggestion-from-a-rubyist/2326/8 "2016-04-25T22:00:20Z")

</div>

That makes it sound much more definitive than anything on this list  
other than a formal review response can ever be. What actually  
happened?

> **···**
>
> on Sun Apr 24 2016, Xiaodi Wu \<swift-evolution@swift.org\> wrote:
> 
> > &nbsp;&nbsp;&nbsp;&nbsp;Anyways, I wouldn’t be surprised if this idea has come up before and has  
> > &nbsp;&nbsp;&nbsp;&nbsp;been rejected, but to me it sounds like a good idea.
> > 
> > Yes, I suggested this a while back, and it was rejected.
> 
> --  
> Dave

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [April 25, 2016, 10:43pm UTC](https://forums.swift.org/t/mutating-non-mutating-suggestion-from-a-rubyist/2326/9 "2016-04-25T22:43:31Z")

</div>

Me:

Unless the functions also return an error, mutating/non-mutating pairs  
of functions return Void/Self (or maybe Optional\<Self\>) respectively.  
Are there other possibilities? But Swift is pretty unique among  
C-family languages in allowing overloaded functions that differ only  
by return type. Besides the loss of clarity to the reader at the call  
site, what are downsides of simply naming both functions exactly the  
same in today's Swift syntax?

You:

I don't think it's really worth exploring much further once you acknowledge  
the loss of clarity to the reader at the call site ;-)

(I think the smiley really seals the deal in terms of definitiveness of  
rejection, no?)

> **···**
>
> On Mon, Apr 25, 2016 at 17:05 Dave Abrahams via swift-evolution \< swift-evolution@swift.org\> wrote:
> 
> > on Sun Apr 24 2016, Xiaodi Wu \<swift-evolution@swift.org\> wrote:
> > 
> > \> Anyways, I wouldn’t be surprised if this idea has come up before and  
> > has  
> > \> been rejected, but to me it sounds like a good idea.  
> > \>  
> > \> Yes, I suggested this a while back, and it was rejected.
> > 
> > That makes it sound much more definitive than anything on this list  
> > other than a formal review response can ever be. What actually  
> > happened?
> > 
> > --  
> > Dave
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![dabrahams](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dabrahams/32/9815_2.png) [@dabrahams](https://forums.swift.org/u/dabrahams)\
**Post date:** [April 25, 2016, 11:21pm UTC](https://forums.swift.org/t/mutating-non-mutating-suggestion-from-a-rubyist/2326/10 "2016-04-25T23:21:25Z")

</div>

Hey, that was just my opinion at the time; that doesn't mean the  
community rejected the idea or the core team rejected the idea [or even  
that I still believe the same thing this week ;-)]

> **···**
>
> on Mon Apr 25 2016, Xiaodi Wu \<xiaodi.wu-AT-gmail.com\> wrote:
> 
> > Me:
> > 
> > Unless the functions also return an error, mutating/non-mutating pairs  
> > of functions return Void/Self (or maybe Optional\<Self\>) respectively.  
> > Are there other possibilities? But Swift is pretty unique among  
> > C-family languages in allowing overloaded functions that differ only  
> > by return type. Besides the loss of clarity to the reader at the call  
> > site, what are downsides of simply naming both functions exactly the  
> > same in today's Swift syntax?
> > 
> > You:
> > 
> > I don't think it's really worth exploring much further once you  
> > acknowledge the loss of clarity to the reader at the call site ;-)
> > 
> > (I think the smiley really seals the deal in terms of definitiveness of  
> > rejection, no?)
> 
> --  
> Dave
