Swift: Array method names are inconsistent regarding mutation

Originator:wetzeal
Number:rdar://19976596 Date Originated:26-Feb-2015 04:53 PM
Status:Closed Resolved:14-Jun-2016
Product:Developer Tools Product Version:Xcode-beta (6D532l)
Classification:UI/Usability Reproducible:Always
 
Summary:
The swift standard library supplies a number of convenient methods for modifying standard structs. These methods appear to be largely consistently named where direct verbs mutate structs, and adjectives return new copies with the transformation applied.

Array has a number of methods which violate this convention.
While `sort` and `sorted` adhere to the convention, `filter`, `reverse`, and `join` do not. Methods such as map which return a collection of elements of a different type than the original are obviously exempt from this expectation.

This makes working with arrays difficult as the documentation must be frequently consulted to determine if a method mutates the array or not. This is particularly bothersome because the compiler provides no warning that `myArray.reverse()` did not do anything.

Steps to Reproduce:
Write code to mutate an array by reversing it.

Expected Results:
The code I expect to mutate the array causes a mutation in place.

Actual Results:
The code silently returned a new array, which was thrown away by the running program.

Regression:
Many languages Swift has heritage from have good conventions for denoting mutability, such as Ruby/Scheme’s `!` suffix.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!