[Swift] Disallow operator overloading outside of system libraries

Originator:amolloy
Number:rdar://17304784 Date Originated:6/13/14
Status:Closed Resolved:As Intended
Product:Developer Tools Product Version:
Classification: Reproducible:
 
One of Swift's more powerful features is operator overloading. It is clear from studying the language and its standard library that operator overloading is key to making much of the language work. Unfortunately, over a decade of experience with crafting and maintaining C++ code has taught me that it is a power best reserved for the authors of the system libraries. 

This is more than a nit. Giving "everyday" programmers the power to overload operators invariably leads to poorly considered difficult to maintain code. True, the intention is to allow more straightforward APIs to things like vector and matrix math libraries, but even within that case operator overloading can be and frequently is abused.

A very common and simple example from C++, overloading the * operator for vectors:

result = vectorA * vectorB;

Initially, this looks fine, but... What operation is actually taking place here? The cross product or the dot product? You can't tell without further investigation - is result a vector type or a scalar type? And so on. And this is an example of what is typically considered a sane, sensible use of operator overloading. 

Swift's operator overloading is more flexible and powerful than C++'s, and the above example could probably be expressed in a better, more readable way with Swift, but that isn't the point. As long as programmers have the ability to overload operators, there will always be ambiguity (for the programmer) about what an operator does at each site where it is used. And with the added flexibility of Swift's operator overloading comes more ambiguity, not less. Swift programmers will quickly find themselves wondering what the heck their colleagues intended when encountering the inevitable questionable use of some operator overload.

Now, consider how this might be done without operator overloading:

result = cross(vectorA, vectorB)

or perhaps (though I wouldn't recommend it):

result = vectorA.cross(vectorB) 

In these cases, the intent of the original programmer is clear the moment you lay eyes on it. There is no ambiguity - this is calculating the cross product of two vectors. It may take 0.5 seconds longer to type (less, probably, with modern code completion) but will save significantly more time during future maintenance.

The desire to be terse is incompatible with the goal of writing lasting, maintainable code.

Please consider reserving the use of operator overloading to the authors of the system library. Programmers tasked with updating and maintaining other peoples' code (where "other people" includes themselves six months prior) will thank you.

Steps to Reproduce
N/A

Expected Results
N/A

Actual Results
N/A

Configuration
Any

Xcode Version
Xcode 6 Beta 1

Additional Notes

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!