Live data from Hacker News

Remove the ++ and –- operators

github.com

141–150 of 249 posts

Re: Remove the ++ and –- operators

#141

Note that if anyone truly misses these operators, you can add them back with a few lines of code. Swift has extensive support for custom operators, and if ++ and -- disappear as built-in operators, you can just implement them as custom operators. I see a lot of people complaining that they like ++ and -- who may not realize that they can still have it if they want it. I get that there are legitimate questions over wh…

That's good. Can you make anything? Like x+ is x+=1, x++ is x+=2, x+++ is x+=3? x+++++++++++++++++++++++++++++

Re: Remove the ++ and –- operators

#144
post #123

Earlier quoted context omitted.

Yes. Anointing "+ 1" as a thing that needs special syntax is weird .

Not necessarily so if you come from a strong Assembly language background. Most processors have a special instruction for incrementing a register that's shorter (and faster) than the more generic ADD instruction. For instance, on a 64-bit x86 machine, ADD RAX,1 is nine bytes long, while INC RAX is one byte. Maybe not as much a win these days, but back when memory was smaller and more expensive, it was worth it.

Doesn't x86 machine code have short-immediate forms? So ADD RAX,1 could be 4 or 5 bytes. Still a win to use INC of course.

Re: Remove the ++ and –- operators

#145

Earlier quoted context omitted.

Huh. C++? You can overload operators, but define your own? I must have missed something.

Well, I suppose you can #define your own?

#define function(arg1, arg2, arg3) exists for creating macro functions, but nothing exists for naturally creating macro unary or binary operators in C and its descendants.

Re: Remove the ++ and –- operators

#146

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…

Sounds fine to me, what changes to a standard tokenizer and parser would you make to carry out your plan?

I'm not saying this is a shining example of brilliant programming, but it is in a semi-working state and something "in a similar style" (that is, a three-pass tokenizer -> LR parser -> LR parser where scope information and other metadata is computed in the second pass and fed into the third) should be able to handle the changes you're talking about.

https://github.com/dbpokorny/autoclave/

Re: Remove the ++ and –- operators

#147

Earlier quoted context omitted.

FWIW, Go linter suggestion is to to use i++ but i += 2. If you write i += 1, it'll suggest you should write i++ instead: main.go:6:2: should replace i += 1 with i++ That said, if it were removed, I'd probably be happy because it makes the language a little simpler, but I don't mind having it too much either, because it's a statement rather than an expression.

wow, now that seems needlessly confusing. "use x += _" to increase x by any amount except one, in which case you use x++"

+= 1 is a pretty common case though, it doesn't seem particularly arbitrary for it to be special. It's not "clean" or "minimal" but it is pretty convenient.

Re: Remove the ++ and –- operators

#148

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…

Another way to express this is, I'd like a language keyword for "left hand side of assignment". Then for x=x+1 I could say x = + 1; It could then get arbitrarily complex. E.g. x = ( + 1) / atan( ) - ; and no matter how complex 'x' was in practice, the compiler (and the code reader) could keep track of what was happening.

I think I get what you mean, but I cannot imagine a single example where your proposed keyword would be a good solution: IMHO, if I need a keyword to make the RHS understandable to the reader and the (much smarter) compiler, a macro or function is called for to encapsulate the RHS in a named and documented unit of code.

Re: Remove the ++ and –- operators

#149
I disagree with almost all of the justifications, but I don't use Swift and mostly program in C++ so I'm pretty biased.

> These operators increase the burden to learn Swift as a first programming language -

Same could be said about almost any other slightly "advanced" feature. Not to mention the fact that I fail to see the difficulty here. Increment/decrement take literally a few minutes to explain and understand.

Actually, it's more difficult to explain the concept of a function call, so let' propose to remove functions instead and stick with goto and global variables because they are easy to explain to a novice.

> Their expressive advantage is minimal - x++ is not much shorter than x += 1.

Increment/decrement looks visually more compact. (x += 1) looks hideous, has this "magic constant" and has 3 extra chars. Not to mention, x++ does have the expressive advantage, because it has entirely different semantics: "return value THEN increment", while x += 1 doesn't return anything in Swift (which is mentioned right in the next entry).

> Code that actually uses the result value of these operators is often confusing and subtle to a reader/maintainer of code. They encourage "overly tricky" code which may be cute, but difficult to understand.

It is not if you actually understand the semantics of those operators. Writing things like "return currentSize++;" is perfectly clear (assuming you know the meaning of the operator) and more concise and less noisy compared to something like "currentSize += 1; return currentSize;".

> While Swift has well defined order of evaluation, any code that depended on it (like foo(++a, a++)) would be undesirable even if it was well-defined.

By that logic, again, function calls should be banned. foo(bar(a), baz(b)) where bar and baz have side effects is also undesirable.

> These operators are applicable to relatively few types: integer and floating point scalars, and iterator-like concepts. They do not apply to complex numbers, matrices, etc.

I don't see how this is relevant at all. There are lots of operations that are only applicable to certain types. It doesn't mean such operations should be removed. You can't compute the sine of a matrix or a complex number yet you wouldn't remove sin(). You can't compare two complex numbers or two matrices but I don't see operators going anywhere.

> Swift has powerful features that eliminate many of the common reasons you'd use ++i in a C-style for loop in other languages, so these are relatively infrequently used in well-written Swift code. These features include the for-in loop, ranges, enumerate, map, etc.

True, but when it does become necessary I'd rather have that functionality available. I see no need to handicap the language this way.

> Having to support these could add complexity to the potential revised numerics model.

This might be the only valid (and the real) reason for removal... but I don't have enough information to comment on this.

Re: Remove the ++ and –- operators

#150

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…

Do you mean something like "compound assignment operators"? Apparently you can define custom operators like: func += (inout left: Vector2D, right: Vector2D) { left = left + right } https://developer.apple.com/library/ios/documentation/Swift/...

Question of clarification: is this the same concept as Python's __iadd__? I was not aware that this was a "thing" in programming language design.

http://stackoverflow.com/questions/1047021/overriding-in-pyt...

Post reply on HN