News for hackers. Stuff that matters.
Re:Remove the ++ and –- operators by jonhohle on Thursday December 03, 2015 @07:08PM (#10673715)
Isn't this how C++ works as well? operator+= is definable for any user defined types and typically you'll add an overloaded operator[] which returns a reference so that things like operator+= works as you'd expect within the context of a collection.

I have some old code somewhere for images which I'm sure does something to the effect of:

    image[x, y] *= scale;
where Image::operator[] returns a Pixel&.
Re:Remove the ++ and –- operators by ridiculous_fish on Thursday December 03, 2015 @07:46PM (#10673878)
Not quite. C++'s references are syntactic sugar on top of pointers: a Foo& always needs a Foo to point to. But Swift's inouts are implemented as a pair of closures: a getter and a setter, which need not just modify a Foo.

Consider the much-hated vector<bool>, implemented as a packed bit array. Since you can't return a reference to a bit, operator[] must create a special wrapper class that holds a reference to the word and the bit offset, and then overload operator= and perform other tricks. It's famously leaky and gross.

In Swift, we can implement this optimization much more cleanly. inout lifetimes are always scoped, and when the scope ends, the new value is written back to the object via its setter. Our `inout boolean` is not backed by a boolean at all: it's backed by a getter and setter which can do arbitrary things, like mask and shift a word.