15 ms·
Remove the ++ and –- operators
- mei0Iesh 11y agoIs this like the Apple idea to have a 1-button mouse? I like to ++ things, because when I'm coding, it fits better with that focused efficient state of mind where you don't have to think about x + 1, you just say ++ and it's bigger. But I'm not an Apple programmer, so maybe this isn't for my style.
- smegel 11y ago> Their expressive advantage is minimal - x++ is not much shorter than x += 1. And there is no ++ equivalent of x += 2.
- cballard 11y agoYes. Anointing "+ 1" as a thing that needs special syntax is weird.
- dragonwriter 11y ago> Yes. Anointing "+ 1" as a thing that needs special syntax is weird. Its not a special syntax for "+1", its a special syntax for the combination of assignment and increment/decrement. The postfix versions (increment/decrement variable and then return the value that was present before the increment/decrement), particularly, are equivalent to: _x = x; x += 1; _x In an environment where that pattern of operations is common, I can see the value of having a concise mechanism for expressing it. OTOH, there's a cost to it, too, and I'm mostly happier without them around.
- cballard 11y agoSorry, you are correct - although I would restate my point with "+= 1" isn't special. 1 is just another number, the fact that it's useful for enumerating arrays in C isn't relevant as Swift provides more native solutions for that, the use of which should be encouraged.
- ars 11y agoHave you never heard the rule that your code should have no numbers in it except 0 and 1? All other numbers should be written as constants instead, and then use the name of the constant instead of the number directly. > Anointing "+ 1" as a thing that needs special syntax is weird. 1 is special.
- cballard 11y agoYes, of course, I use constants in all cases. Why is adding 1 to something special, compared to adding 2 to something? The #1 use case, by far, is C-style for loops, which aren't used in idiomatic Swift code.
- ars 11y agoI said why: The 2 should be a constant as a name, but the 1 can be a bare 1.
- spc476 11y agoNot 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.
- cballard 11y agoIn a high-level language, this is the job of the compiler, though. If the compiler isn't generating those instructions for cases where the increment can be proven to be 1, I would consider it a bug.
- DiThi 11y agoNowadays, a compiler that doesn't optimize a "+1" is a bad compiler.
- spc476 11y agoThese days yes. But when C was first being developed, when you might have had 64K of memory for everything? Back then it might have been a sane choice.
- JoeAltmaier 11y agoDoesn'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.
- throwaway999888 11y agoAny optimising compiler for a mainstream language that can't do this optimisation reliably in this day and age is crap.
- mattst88 11y agoCompiling #include <stdint.h> uint64_t inc(uint64_t x) { return x + 1; } With -O0 gives me: 48 83 c0 01 add $0x1,%rax and with -O2: 48 8d 47 01 lea 0x1(%rdi),%rax That is, both are four bytes.
- revelation 11y agoI'd say the expressive advantage of x += 1 is zero since it's not an expression.
- kamaal 11y agoPerl 6 user defined operators are just made to order for you.
- ChrisLTD 11y ago"Drop one of x++ or ++x. C++ programmers generally prefer the prefix forms, but everyone else generally prefers the postfix forms. Dropping either one would be a significant deviation from C." Funny given the name of the language.
- warmwaffles 11y agoC++ is not even a good increment of C. Yes it had a lot of inspiration from C, but in all seriousness, it should not have been called C++ since it is not backwards compatible with C without some serious work. Honestly longest running ironic language name.
- richardwhiuk 11y agoSorry, the lifetime winner of that award is JavaScript.
- clord 11y agoActually it's a good name. C got incremented, and we keep original C locally while some weird side effects no one really wanted keeps happening in committee until the end of time.
- DOUBLESTDRDII 11y agoVOTES-- MORE SERIOUSLY....CODING CAN BE ABOUT AESTHETICS. YES, JUST BECAUSE. CODE CAN LOOK GOOD AND THAT IS A VALUE. BUT ALSO, AESTHETICS CAN HAVE A NON-MEANINGLESS AFFECT ON COMPREHENSION, READABILITY, CODING-PLEASURE AND PRODUCTIVITY. SO MONEY, BASICALLY. PERSONALLY, I LOVE THE ++ AND -- AESTHETIC. IT'S JUST A COOL GLYPH/DIGRAM/IDIOM. BRING BACK THE QUIRKINESS ( OKAY, NOT TO THE PERL-LEVEL ) BUT BRING IT BACK!
- filthydumbidiot 11y agoJudging by your capitalization, you must be of the BASIC school of aesthetics...?
- springogeek 11y agoIt could be FORTH.
- TazeTSchnitzel 11y agoOr PHP, if they're one of those very few people who still writes it like that.
- Practicality 11y agoIs this some kind of troll/satire on aesthetics?
- manaskarekar 11y ago> Their expressive advantage is minimal - x++ is not much shorter than x += 1 Personal opinion, but I definitely miss '++' every time I'm doing '+=' in Python.
- tostitos1979 11y agoAgreed. In the list of disadvantages, we don't see "many professional programmers expect the ++ and -- operator" or "beginners will be confused because old code examples on the web will stop working".
- joshavant 11y agoThis argument feels like 'legacy for legacy's sake'.
- gozo 11y agoFrankly I think discussions like these show how not all programmers are engineers. Cutting down on complexity and ambiguity is a good thing. Because even if you can handle something, when you have teams of 100+ people with 100 000+ lines of codes, all those potentials for errors quickly adds up.
- legulere 11y agoWhich is actually quite common. Unicode is ASCII compatible and on Windows you still have to do carriage return before a line feed, although there are no teleprinters involved.
- redthrowaway 11y agoWhich wipes out the "easier to learn as a first language" point, and then some. Copy-pasting code from stack or somesuch only to have it not work and not know why is incredibly frustrating for a beginning programmer. I certainly found that with wpgtr back in the day.
- delinka 11y agoI think I'd argue with the "not much shorter" because it's less typing to perform a `++` versus `+=1`; I hold shift, I type the = key, obtaining the +, then I have to pause typing long enough to release the shift key, type the = key again, and now my other hand has to get involved and type a 1. And that's without the spaces.
- jheriko 11y agopost increment/decrement is definately worth removing imo. so few people understand its behaviour and it enables some really revolting and difficult to read things. i think a lot of people learned this in the 80s, 90s and 00s even... its not a new revelation. the pre-increment/decrement though? i think having these is important. there are plenty of contexts away from numbers where they make sense (things can be ordered and not have a concept of addition)
- arbitrage 11y ago> it enables some really revolting and difficult to read things. for others, though, it really enables the writing and reading of some very elegant and concise code.
- jheriko 11y agousually when people say this what they mean is revolting and difficult to read things. (i'm not 100% sure this means you, but all of my experience tells me this is likely the case, sorry to have to be critical) i've heard this sort of thing about *a++ = *b++; and variants thereof, which is the classic example of this. a lot of the times i have seen this stuff it is in the middle of even more revolting code too... like some 1000 line function (which could be 10 functions and has comments inside of it that delineate the obvious splits) with a gigantic switch statement (which could be a class, or in C a function pointer), containing loops, where there are 20 lines like the above but even more difficult to read in a row (which could have been a nested loop with identical logic referring to some constant data) - to think of the most recent example i faced. sadly i've seen this sort of stuff so many times for so long that i find it easy to read, but it is intrinsically difficult involving precedence and the post-increment gotcha, and it gets worse if there is even more being done than just a straight copy.
- alricb 11y agoWhat about a function named succ or next? They would make things a bit more explicit, and succ has its own tradition.
- Alupis 11y agoWhy 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...
- syncsynchalt 11y agoBecause they can constrain the design of the language, and make it more complex to reason about the language. Look at the language necessary to explain their pitfalls in the C standard, or the extra copy needed in operator++(int) to support post semantics in C++. They also make the tools that consume that language slightly more complex. It doesn't sound like much, but in aggregate it's the difference between writing a parser for Java vs. a parser for C++.
- cballard 11y agoBecause non-advanced users will incorrectly abuse them. That's why ARC doesn't let you call retain and release manually, even though there's no technical reason it couldn't.
- JustSomeNobody 11y agoSo? Advanced developers abuse languages all the time. This isn't a valid argument.
- cballard 11y agoMaking the language difficult for advanced developers to abuse is practically an explicit goal of Swift. I mean, its predecessor allowed you to mutate your class hierarchy at runtime, and this was a thing that people commonly did! "You can't do that" is a powerful tool for making code safer.
- Alupis 11y ago> Because non-advanced users will incorrectly abuse them. I don't see how this is a valid reason to remove the operators completely. Beginners will get it wrong sometimes, so we should remove it? Beginning users are likely to make all kinds of errors, so the implication would be maybe we should remove the entire language...? Not to mention, pre-decrement and post-decrement are not complicated concepts. There are many valid reasons to use both, although post-increment/decrement seems to be the more common use.
- rocky1138 11y agoI'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.
- SamReidHughes 11y agoIn Go, where they're a statement, they're fine. But as an expression, but stuff can happen. In non-contrived, real-world, historical examples.
- Gibbon1 11y agoWas 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.
- Retra 11y agoAll the examples where they're useful are contrived. And simplicity is an actual goal of computer language design.
- mattst88 11y agoThat's quite a claim. I agree that most uses of these operators as an expression are not wise. Some are though. As a counter-claim, I offer: array[i++] = this; array[i++] = that; array[i++] = the_other; which also extends nicely when some of those elements are optional.
- SamReidHughes 11y agoFor what it's worth, you could always make a function postincr and write array[postincr(&i)] = ...; if you're doing enough of those that it's worth it.
- grabcocque 11y agoDoing stuff because that's the way C does it is a surefire route to unintentional autoemasculation.
- tizzdogg 11y agoDoing stuff because that's the way elisp does it is a surefire route to unintentional autoemacsulation.
- cballard 11y agoWhile 4 touches on this a bit, I think another reason is that mutable value types are generally harmful. Save mutability for compiler optimizations and write your code with "let".
- oofabz 11y agoYou need mutability to write for loops.
- cballard 11y agoC-style for loops are discouraged in good Swift code. Use map, ranges, or for/in instead.
- usefulcat 11y agoSo.. get rid of 'x += 1' as well?
- cballard 11y agoYes! And local "var". The optimizations that "var" allows should be the job of the compiler. It's still needed at the non-local level on reference types, but could possibly be disallowed in structs as well.
- mwsherman 11y agoGo’s compromise here is that these operators are statements, not expressions. There is no return value. This makes it semantically identical to i += 1. It keeps some sugar while removing some classes of gotcha.
- throwaway999888 11y agoJust having `i += 1` would seem more in line with Go's professed Simplicity philosophy. When both `++i` and `i += 1` mean the same thing, you might as well drop one of them. `++i` (and/or I guess `i++`) is barely syntactic sugar.
- AnimalMuppet 11y agoi++ is different from i += 1 in this way: += could be i += 2 or i += j or something else. I have to pay attention to be sure. But i++ can only add one.
- throwaway999888 11y agoYes, 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 something else that is wrong. But in this case the space isn't across a method, or a block. The space between the destination (i) and the increment number (1) is literally just 4 characters.
- AnimalMuppet 11y agoBut 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.)
- dboreham 11y agoGood. I've always presumed ++ is in C because processors had an increment instruction that was faster than add back then, and for the PDP-11's autoincrement addressing mode. Presumably Dennis' compiler would have been that much bigger in order to spot these optimizations vs having the programmer explicitly invoke them -- perhaps big enough to no longer fit in memory.
- mturmon 11y agoPeople suppose this, but that's not quite how it happened. C came from B, and B had ++ also. But B came before the PDP-11. The PDP-7 had hardware that supported auto-increment-by-one, and Ritchie suggests that's where ++ came from. The PDP-7 assembler manual is at http://bitsavers.informatik.uni-stuttgart.de/pdf/dec/pdp7/PDP-7_AsmMan.pdf http://bitsavers.informatik.uni-stuttgart.de/pdf/dec/pdp7/PD..., but I can't see any reference to this auto-increment hardware, or to an assembler directive using it. What I'm getting at is that there appears not to have been an assembly language construct for auto-increment on the PDP-7 at all, even though its hardware supported it. The hardware "auto-indexing" is described at page 3-12 of the PDP-7 architecture document, http://bitsavers.informatik.uni-stuttgart.de/pdf/dec/pdp7/F-75P_PDP7prelimUM_Dec64.pdf http://bitsavers.informatik.uni-stuttgart.de/pdf/dec/pdp7/F-... . It is enabled only for about 8 special memory addresses in each 8K of address space, so it is weird and handicapped by comparison with the PDP-11. See Ritchie's history (http://www.bell-labs.com/usr/dmr/www/chist.html http://www.bell-labs.com/usr/dmr/www/chist.html) -- "Thompson went a step further by inventing the ++ and -- operators, which increment or decrement; their prefix or postfix position determines whether the alteration occurs before or after noting the value of the operand. They were not in the earliest versions of B, but appeared along the way. People often guess that they were created to use the auto-increment and auto-decrement address modes provided by the DEC PDP-11 on which C and Unix first became popular. This is historically impossible, since there was no PDP-11 when B was developed. "The PDP-7, however, did have a few `auto-increment' memory cells, with the property that an indirect memory reference through them incremented the cell. This feature probably suggested such operators to Thompson; the generalization to make them both prefix and postfix was his own. Indeed, the auto-increment cells were not used directly in implementation of the operators, and a stronger motivation for the innovation was probably his observation that the translation of ++x was smaller than that of x=x+1."
- homulilly 11y agoI strongly disagree with this. Being able to do a very simple task like increment or decrement a number inline is useful and reduces clutter and I've always found the idea that the operator complicates things ridiculous. The way they work has always been obvious to me since I first learned to program in PHP as a tween.
- ra1n85 11y ago"These operators increase the burden to learn Swift as a first programming language" It looks like they may be trying to make it easier for current tweens that would have otherwise learned PHP. Don't see the value in this. These operators are terse, well known, and are implemented in a wide array of languages.
- JoeAltmaier 11y agoWell known for old timers. But some folks have an actual issue understanding them. For those of us that are comfortable; fine, no issue. But they are a little strange; the order of operations in a complex expression can be hard to tease out for novices. Are they worth it?
- Nadya 11y agoWhat about for beginners like me who instantly understood `++` and `--` as increment/decrement but avoided `+=` and `-=` for being rather nonsensical? This half-reeks of something that a professional/advanced user thinks trips up beginners without actually tripping up beginners. Or they have an anecdotal sample size of n < 10 and decided since it confused 1 person that 10% of people get confused.
- cballard 11y agoApple has a long history of readability over terseness. "plus plus" doesn't mean anything. It might even be confusing that it doesn't add 2.
- dstanko 11y ago
- hoverbear 11y agoWe don't have `++` and `--` in Rust either, and I really don't mind. The amount of times I've went to do a `++` where a `+= 1` was clutter has been perhaps one or two times. Then again, my most common use for `++` was `for (;;) {}` index, and we have better in these new languages like Rust and Swift. There are millions of people in this world trying to learn to code and understand the layers upon layers of cruft and quirks we've added. I don't mind the `++` or `--` operators, but I certainly don't mind them being gone either. They add extra complexity to code which is already arguably too complex. In any case, I favor understandability over guesswork, and `++` is another odd cornercase which you have to mentally `grep` over.
- marcosdumay 11y agoOh, I've just closed a Haskell window, saw the title and thought "how would I concatenate strings?", but I'm digressing... But no, not only for for(;;). There's also the too common array traversal: while(something) a[x] = b[x++]; And lots and lots of ugly but useful uses in pointer arithmetics where they make things clear. They have no place in any other language, but I'd really miss both operators in C.
- yokohummer7 11y ago> while(something) a[x] = b[x++]; Wait, isn't this an undefined behavior? Is the order of evaluations of `x` in the left side and `x++` in the right side defined? I'm always confused at things like this!
- marcosdumay 11y agoI dunno. I always thought the ++ operator was evaluated after the entire statement, and every compiler that I could get my hands on behaved that way. But, well, the specs are another matter entirely.
- marcosdumay 11y agoJust to update, yes, it's undefined behavior. Every compiler seems to act the same way today, but some specs oriented optimizer might play havoc with that line.
- mikejmoffitt 11y ago"These operators increase the burden to learn Swift as a first programming language" Come on. There are legitimate reasons not to carry one language's expressions to another, but this isn't a good one. Most people first learn of ++/-- as "By the way, you can use ++ as a shortcut to add one" and leave it at that.
- JoeAltmaier 11y agoBut its more complex than that- there are the prefix and postfix forms, which both leave the base incremented but return a different value (before or after the increment). That's the complexity that trips up novices.
- JoeAltmaier 11y agoRemoving ++ 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 is short but (rec)FnRec(iRec) = (rec)FnRec(iRec) + 1; is not short. And the compiler gets some great hints when the '+=' form is used (don't calculate the reference twice). I just want that for my stuff too! E.g. I may have a transformation method Xform(Foo&, Bar) that returns another Foo&. I want to say foo = Xform(foo, bar); but with 'foo' replaced by a complex reference. I have to say FnFooRef(iFoo, "primary", red) = Xform(FnFooRef(iFoo, "primary", red), bar) instead of FnFooRef(iFoo, "primary", red) Xform= bar; I want my own 'operators' with the 'op=' syntax supported. Probably its not terribly useful any more; languages support immutable data and tuples and so much that don't work this way at all.
- jsprogrammer 11y ago>Why does '+' (and friends) get this special form, but not my functions? Your functions can get that (and other) special form. You need to use a language where you can define your own operators. Have you looked at C++, C#, Haskell, or LISP?
- JoeAltmaier 11y agoHuh. C++? You can overload operators, but define your own? I must have missed something.
- janvidar 11y agoWell, I suppose you can #define your own?
- winter_blue 11y ago#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.
- ape4 11y agoGet rid of `x += 1` also. Its just shorthand for `x = x + 1`. /sarcasm
- nevereverreg 11y agoWhy remove x++ and not x += 1? Demand X = X + 1. For clarity/readability/formatting.
- dragonwriter 11y ago> Why remove x++ and not x += 1? A key part of the reason for removing the ++/-- set of operators seems to be that they conflict with Swift's pattern of mutating operators returning void. (An alternative offered was revising them so that they followed that pattern.) +=/-=, like simple assignment, do not have that problem.
- vvpan 11y agoWell, I assume that it's because myObject.someVariable = myObject.someVariable + 1 is harder to read than myObject.someVariable += 1 Also it's annoying to type and increases the chance of typos.
- Animats 11y agoI tend to agree with removal. Nobody writes for (i=0;i<n;i++) *a++ = *b++; any more.
- mFixman 11y agoAll the cool kids use Null-terminated strings now. while (*a++ = *b++);
- brandonmenc 11y agoI have rarely used ++ and -- outside of inc/decrementing pointers and loop counters in C code. They probably won't be missed in Swift.
- bcoates 11y agoWhile I agree that having both pre- and post- versions is confusing overkill, ++ is no more sugar for x += 1 than x+x is sugar for x*2. Increment is the fundamental operation, += is sugar for repeated incrementation, not the other way around.
- seivan 11y agoGood, I always sticked to += 1 because it felt better. Weird to see I'm not alone. I could never really memorise the difference between i++ and ++i because it just seemed like such an obscure thing to have. It's really really weird (and cool) to see someone suggesting removing it.
- bootload 11y ago"When the return value is needed, the Swift += operator cannot be used in-line, since (unlike C) it returns Void." Why was this decision made?
- legulere 11y agoSo you can't do this: a += b -= c; Don't ask me in which order it executes, I would need to read the C standard again and wrap my head around how this case is described therein. I'm also not sure wether this is defined or undefined behaviour: a += b -= c *= a;
- JoeAltmaier 11y agoNow throw a few ++ and -- in there, and boy howdy.
- bootload 11y ago"So you can't do this: a += b -= c; Don't ask me in which order it executes," thx for the reply @legulere, I can now see the void, why would the language allow syntax that smells so bad? Can you do this in "c"?
- ninjakeyboard 11y agoscala has no ++ or -- operators. It's a-okay. Swift is shaping up to look a lot like scala actually. The biggest problem with ++ and -- operators is that they hide a side-effect in another unrelated operation. In a language that supports more functional approaches at the outset, it makes sense to just omit these operators I think.
- imdsm 11y ago> Keep both x++ and ++x in the language, even though they do the same thing. ?
- dragonwriter 11y ago> > Keep both x++ and ++x in the language, even though they do the same thing. > ? That was a subalternative underneath the alternative of changing the operators to return Void like other assignment operators. ++x and x++ differ in that the former returns the value after the increment, the latter the value before; if they return void, all they do is the increment with no difference.
- imdsm 11y agoThis a delayed response for which I apologise, but thank you for yours, this makes complete sense now. I must have missed this at the time. Makes sense now.
- deleted 11y ago[deleted]
- mikeash 11y agoNote 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 whether it's wise for the community and such, but for your own work, you can still have it if you want it.
- mei0Iesh 11y agoThat's good. Can you make anything? Like x+ is x+=1, x++ is x+=2, x+++ is x+=3? x+++++++++++++++++++++++++++++
- mikeash 11y agoYep: http://swiftstub.com/43404468 http://swiftstub.com/43404468 Obviously you should be careful not to commit such atrocities in any code that might ever see production.
- ozy123 11y agomuch ado about nothing
- ozy123 11y agomuch ado about nothing
- scolfax 11y agoFTA: Advantages: "The primary advantage of these operators is their expressive capability" Disadvantages: "Their expressive advantage is minimal"
- logn 11y agoI had no idea there was even a debate about this let alone people passionate about each side. This is bike shedding right?
- nikdaheratik 11y agoIt is indeed.
- RotsiserMho 11y agoMaybe. No one really wants to make a new C++ without being careful not to make something as convoluted as C++.
- deeteecee 11y agolol.
- paulgayham 11y agoThis: > Their expressive advantage is minimal - x++ is not much shorter than x += 1. is not a disadvantage, that's a small advantage that tells me you had trouble making your list of disadvantages.
- nice_byte 11y agoI 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 < and > 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.
- deleted 11y ago[deleted]
- jakejake 11y agoIf there's some technical or performance reason for getting rid of this as far as the swift compiler goes, then I guess whatever just tell me how to increment a number and that's what I'll do. But to try to say that x++ is complicated to understand? It's no more complicated than a lot of operations. I don't think anyone would intuit what x%2 means either. There are things that you can get just by looking at it, and others that you just have to learn once, then you go "oh, that's what it does" and move on.
- nraynaud 11y agoI would just make a++ an instruction, and remove the rest.
- jhallenworld 11y agoThe 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 bunch of variables: a:b:c:d:a transfers all the contents to the left, and puts the old value of a into d. The . operator should have an assignment form as well, so that you can say: p .= next instead of p = p.next Now you can traverse linked lists very concisely: for (p = first; p; p .= next) serialize(p); Of course .: will have the expected meaning: return old value of left side, then replace it with one of its members. So you can say: while (p) serialize(p.:next)
- SamReidHughes 11y agoWhy do you want this?
- jhallenworld 11y agoSame reason mathematicians have symbols like: ∀, ∃, ∴, ∬, etc. In fact now that Unicode is universal we should use all of these operators too :-)
- codeshaman 11y agoThe ++ 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.
- personjerry 11y agoIt really bothers me that the title should say "--" but instead says "–-" which consists of two distinct characters.
- mmillis1 11y agoI 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?
- Retra 11y agoThe first thing I learned when programming was GOTO and raw pointer handling. Both are simple. Both are pretty stupid.
- wolfhammer 11y agoWhy not noPlusPlusMathObject.addOne(referenceFor x)?
- bphogan 11y agoOne of the reasons they state is that ++ makes it harder for beginners. I just taught ++ and -- to 1st semester students last week. There were no problems.
- mayoff 11y agoTime that could have been spent teaching them something else useful, if your language didn't have ++ and --. Even if all features of your language are easy, teaching/learning each feature takes time.
- bphogan 11y agoMy point was that it's a poor reason for removing it from a language. Believe it or not the concept of x = 5 is harder to understand. They know that = means equality, because of algebra. I have to retrain prior knowledge, and since humans rely on prior knowledge to learn new things quickly, it's more difficult. Having to explain == or .eq or, god forbid, === takes way more time to explain than: "x = 0". x++ adds 1 to x. So when you need to increment by one, you can do x = x + 1, or you can do x++ which is a shorter way." Boom. Done. Not exactly a good reason for taking it out of a programming language. That's all I was saying. Do I care that Swift has it or not? Nope. But I just thought this particular reason was silly.