Live data from Hacker News

Remove the ++ and –- operators

github.com

151–160 of 249 posts

Re: Remove the ++ and –- operators

#153

Removing ++ et al: ok, they have their problems. But retaining x += 1? That one has always annoyed me. Why does '+' (and friends) get this special form, but not my functions? Just because they're built-in. They are useful; no argument there. Largely because 'x' can be a complex reference, and you don't have to type it twice (and the compiler doesn't have to figure out that its the same expression twice). E.g. x=x+1 i…

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 add a mutable property highHalf on UInt64:

    extension UInt64 {
        var highHalf : UInt32 {
            get { return UInt32(self >> 32) }
            set { self = (self & 0xFFFFFFFF) | (UInt64(newValue) 
Now you can do this:

    var x = UInt64.max
    x.highHalf -= 1
So complex references can be used for any inout. That's how array indexing works.

Swift is the first language I've encountered that works this way; are there others?

Re: Remove the ++ and –- operators

#154
post #140
post #128

Earlier quoted context omitted.

> If you leave them in they're one more special case that everyone has to memorize before they can maintain someone else's code It's literally no different than having to remember what "+=" does... which is what's being proposed as the alternative. > Reading is more important than writing I would agree, but "x++" is not difficult to read. It's more concise than having to read "x += 1". Or worse, beginners will think…

> It's literally no different than having to remember what "+=" does... which is what's being proposed as the alternative. You already have to remember what "+=" does though. If they kept ++ you'd have to learn both.

I don't get the argument... you have to learn what "+" does, so why not remove that and just have "-" since you can subtract negative numbers to add. The line of reasoning that it is more to learn is ridiculous. The concept of incrementing or decrementing a value is trivial. If "++" seriously trips you up as you learn to program, perhaps programming isn't for you...

"+=" is no better.

If you're going to campaign against "++", might as well go all the way back to BASIC days and only allow "x = x + 1"

Re: Remove the ++ and –- operators

#155

Earlier quoted context omitted.

Yes, you actually have to read the statement `i += 1` in order to know what it does. I don't see how that is an argument. It's barely longer than `i++`, in any case. I could buy the argument if we were dealing with indexes or numbers where you had to read/hunt down occurrences of them to make sure they are what you expect. Like: make sure that a loop with a lot of indexes don't overincrement (out of bounds) or does s…

But the point is that with ++, I don't have to look at all at the increment - I know what it is. The issue isn't how far away I have to look, it's that I have to look at all . (Or I don't look, and just assume that it's an add-one because that's what it always is, and then wonder why my loop doesn't work right.)

> (Or I don't look, and just assume that it's an add-one because that's what it always is, and then wonder why my loop doesn't work right.)

How often do your loops depend on incrementing a variable by one? In _Swift_ code.

Re: Remove the ++ and –- operators

#156

Removing ++ et al: ok, they have their problems. But retaining x += 1? That one has always annoyed me. Why does '+' (and friends) get this special form, but not my functions? Just because they're built-in. They are useful; no argument there. Largely because 'x' can be a complex reference, and you don't have to type it twice (and the compiler doesn't have to figure out that its the same expression twice). E.g. x=x+1 i…

>>Why does '+' (and friends) get this special form, but not my functions?

You are right, take a look at Perl 6 infix operators. You can define your own operator(treat it like a function, as such) and then use it.

https://perl6advent.wordpress.com/2013/12/22/day-22-a-catalo...

Re: Remove the ++ and –- operators

#157
post #126

Earlier quoted context omitted.

I didn't know we were making comparisons between Swift and C. Most people would agree C lets the developer do a lot of terrible things (by design, since some of those terrible things are necessary for low level stuff, which C was targeted at). Pointers are confusing for a lot of people. Which is why almost no modern language uses them. Comparing "++" with C pointers is not a valid argument. Even so, I would never arg…

This operator was popularized by C, so comparisons with C make sense. Of course you wouldn't remove pointers from C, the language would barely work if you did. Confusing C-isms like pointers are what Swift is trying to get away from. This is another case of that. "We're making a new language, it isn't C, but will share some characteristics. Which should those be?" is the question being asked here.

It seems to me, we're trying to make Swift "beginner-proof".

The problem, Swift is unlikely to be a "beginner's language"... since it's predominantly (at least for now) an Apple language; used almost exclusively for making iOS apps.

To make an iOS app, you have to have an Apple Developer license, be paid up on fees, own a very expensive Mac, and pay Apple to put your app in the store.

None of which is something a beginner, just starting to learn programming, is likely to do.

Re: Remove the ++ and –- operators

#158
post #116

Removing ++ et al: ok, they have their problems. But retaining x += 1? That one has always annoyed me. Why does '+' (and friends) get this special form, but not my functions? Just because they're built-in. They are useful; no argument there. Largely because 'x' can be a complex reference, and you don't have to type it twice (and the compiler doesn't have to figure out that its the same expression twice). E.g. x=x+1 i…

Perl6 lets you define your own operators [0]. I haven't seen them used enough to know how it affects code readability, though. [0] http://doc.perl6.org/language/functions#Defining_Operators

Code readability will increase, having an operator with tightly defined input(s) type(s) will in fact greatly increase readability and reusability.

Re: Remove the ++ and –- operators

#159
post #5

Why not leave those operators for more "advanced" users? I also contest "++" and "--" operators are seriously tripping up "beginners". In most programming books, it's taught a few chapters in...

Because beginners could get confused when seeing the operators in other people's code, for example. I remember the day I first saw a '++'. It was a rainy night, and I was inspecting a colleague's for loop, when suddenly a '++' operator appeared. I asked myself, "What in the holy bacon of Zeus is that?" So I looked it up in my book. My head almost exploded, and I asked myself if I should quit programming immediately.…

I'd hate to think what would happen if one were to run into a ternary operator!

Re: Remove the ++ and –- operators

#160

Removing ++ et al: ok, they have their problems. But retaining x += 1? That one has always annoyed me. Why does '+' (and friends) get this special form, but not my functions? Just because they're built-in. They are useful; no argument there. Largely because 'x' can be a complex reference, and you don't have to type it twice (and the compiler doesn't have to figure out that its the same expression twice). E.g. x=x+1 i…

>But retaining x += 1? That one has always annoyed me. Why does '+' (and friends) get this special form, but not my functions?

The reason is that I don't want to see you escape your strings by writing

  input escapequotes= input
That's why. come on. who wants to read that?
Post reply on HN