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

Daniel Steinberg daniel at dimsumthinking.com
Thu Apr 21 12:08:31 CDT 2016

Pardon me if this has been raised before.

I gave a short presentation at our Cleveland CocoaHeads this week on what is coming in Swift 3. One of the attendees stayed behind to ask about the naming guidelines for mutating vs non-mutating. He is fairly new to Swift - coming from Ruby. I have no Ruby experience but am passing his thoughts on to this list.

He said that in Ruby they decorate the name with a symbol (I believe in their case it is “!”) to distinguish between the two. Although usually I’m not a fan of such naming conventions, we do something similar with inout parameters.

For example, if we have

func myFunc(param: inout String) { …}

we call it like this (using the Swift 3 first label convention)

myFunc(param: &aName)

We use the & to signal that the value of aName might be changed by the call to myFunc().

Similarly, instead of settling on a naming convention for verb vs verbed/verbing we could name the methods descriptively and require a symbol (here I use & but only for illustration) to distinguish between mutating and non-mutating

so we would have 



sortedArray = myArray.sort()

Xcode and other tools could enforce this naming pattern and warn us that a mutating method must end in “&” and that a non-mutating method is not allowed to.



More information about the swift-evolution mailing list