[swift-evolution] Overloading assignment operator

John McCall rjmccall at apple.com
Sun Dec 6 23:54:15 CST 2015

> 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.

The way to fix your problem in a less ad hoc way is to allow user-defined implicit conversions, which is something we’ve talked about already in a different thread.  What I said there was that it will require a lot of very careful language/type-checker design work.  Here, I’d like to identity another problem: the behavior of implicit conversions can be very confusing for non-experts to reason about, and in the context of a language with a fair amount of other subtle behaviors (e.g. due to overloading), that’s a very dangerous thing to bring in.  Explicit type coercions are much easier for non-experts to reason about.


More information about the swift-evolution mailing list