[swift-evolution] Overloading assignment operator

Don Wills don.wills at portablesoftware.com
Mon Dec 7 00:17:17 CST 2015


John,

> On Dec 6, 2015, at 10:54 PM, John McCall <rjmccall at apple.com> wrote:
> 
>> On Dec 6, 2015, at 5:44 AM, Don Wills via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> The ability to overload operators is very useful.  However, that utility is diminished without the ability to overload the simple assignment operator ( = ).  I vaguely recall reading somewhere that there is a reason for this having to do with syntax ambiguity.  Can this problem be solved so that = can be overloaded?
> 
> In your specific case, I think Alex Lew is completely correct: you really don’t want a ton of overloaded operators, you just want your decimal type to be literal-convertible.
> 
> But to answer the question more generally, no, I can’t foresee us ever allowing the overloading of =, no.  = always means simple initialization/assignment in Swift; that is, it always simply propagates values around.  That’s a very fundamental language concept to mess around with.

I guess I missed the subtlety that in Swift the statement "x = y;" is fundamentally different than "x += y;".  That surprises me.  And it fails one of the basic principles that I've always applied to software design:  The Principle of Least Astonishment.

Thanks for your response, even though it is quite disheartening to me.  As I get to know the nuances of Swift, I'm starting to think that moving to Swift might be too big of a stretch for my programming staff.

Don Wills


More information about the swift-evolution mailing list