Live data from Hacker News

Remove the ++ and –- operators

github.com

181–190 of 249 posts

Re: Remove the ++ and –- operators

#181

The postfix versions of ++ and -- have no simple analogue. I propose fixing this: z = (a += 1) means z gets the new value of a, but z = (a +: 1) means z gets old value a, same as in z = a++. Likewise, z = (a : 5), means that z gets old value of a, then a gets 5. This makes for a nice family of operators. For example, to swap the contents of two variables you can say: a:b:a (or a=b:a). Also you can rotate through a bu…

Why do you want this?

Re: Remove the ++ and –- operators

#182
The ++ operator is one of the main features that brought me to C and C++ when I was just a kid. Basic and Pascal didn't have it and it looked so cool ! Then I had to learn the rest of the language which goes around the ++ operators, but I'm not kidding ==> the ability to write code using ++ or -- was the shit back then.

To me, ++ is the signature operator of the C language AND the operator in the title of the compiler that Swift is written in: C++

I guess it wasn't just me who liked it quite a bit ;).

After so many years I still see a bit of magic in this strange repetition of a character..

So I think they should definitely stay, even if (because) it's not the most pragmatic thing to do.

Re: Remove the ++ and –- operators

#183

The postfix versions of ++ and -- have no simple analogue. I propose fixing this: z = (a += 1) means z gets the new value of a, but z = (a +: 1) means z gets old value a, same as in z = a++. Likewise, z = (a : 5), means that z gets old value of a, then a gets 5. This makes for a nice family of operators. For example, to swap the contents of two variables you can say: a:b:a (or a=b:a). Also you can rotate through a bu…

Why do you want this?

Same reason mathematicians have symbols like: ∀, ∃, ∴, ∬, etc.

In fact now that Unicode is universal we should use all of these operators too :-)

Re: Remove the ++ and –- operators

#184
post #137
post #105

Earlier quoted context omitted.

Then I take it you don't buy into the Python philosophy of "There should be one-- and preferably only one --obvious way to do it." https://www.python.org/dev/peps/pep-0020/

To me, ++ is the one obvious way to increment. I use for loops constantly and think of incrementation as a different thing from addition.

In higher level languages, loops are not about incrementing.

Re: Remove the ++ and –- operators

#186
I feel stupid for asking but is this really a topic of discussion? ++ and -- were one of the first things I learned when starting to program. Its simple and quick to write.

Is this a thing some "new" programmers don't understand what is going on?

Am I taking crazy pills?

Re: Remove the ++ and –- operators

#187

Earlier quoted context omitted.

In Swift, += is a separate operator from +, and is defined within the confines of the language. It's not privileged in any particular way: func +=(inout lhs: Int, rhs: Int) and you can just implement this. You might object that special forms are still privileged, e.g. foo[idx]+=1. But in fact these are not special: one of the surprising features of Swift is that inouts work on computed properties. For example, say we…

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

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

Re: Remove the ++ and –- operators

#188

I've never understood the obsession with avoiding ++ and --. Douglas Crockford advocates the same thing. All of the examples of why it's bad are contrived and set up to fail. In the majority of real world instances, it's just us incrementing or decrementing a single variable.

In Go, where they're a statement, they're fine. But as an expression, but stuff can happen. In non-contrived, real-world, historical examples.

Was just thinking in moldy old c code the two places to use ++ or -- was when either incrementing pointers or indexes.

The other is ++ and -- also map directly to old school assembly language instructions. Helpful when writing the back end of old school compilers.

Probably doesn't make much sense in a language with foreach() or dictionaries, etc.

Re: Remove the ++ and –- operators

#189
post #115

Earlier quoted context omitted.

Each language will have a different meaning of what exactly it means to "define your own". C++ restricts which symbols may be operators, but, you can "define your own +".

In practice it's very useful to distinguish between a language which "just" has operator overloading vs a language that allows custom user-defined symbolic operators. The existence of the latter is actually one of the few complaints that I have about Swift so far, but I'm sure many people will ardently disagree with me here. :P

It's a wonderful feature for writing DSLs, but it's also confusing when you intermingle DSLs with the standard language. It really tends to balloon the amount of information you need to know to read others' code.

Re: Remove the ++ and –- operators

#190

I feel stupid for asking but is this really a topic of discussion? ++ and -- were one of the first things I learned when starting to program. Its simple and quick to write. Is this a thing some "new" programmers don't understand what is going on? Am I taking crazy pills?

The first thing I learned when programming was GOTO and raw pointer handling. Both are simple. Both are pretty stupid.
Post reply on HN