digitalmars.D - Restricting ++ and --
- bearophile (15/15) Oct 25 2009 This post is born from a bug I've just removed.
- Vladimir Panteleev (6/13) Oct 25 2009 No way. I've written items[itemCount++] way too often to give that up.
- d-noob (2/18) Oct 25 2009 Apparently you want to change postInc/Dec to be statements to prevent th...
- bearophile (8/9) Oct 25 2009 It breaks C compatibility, but it doesn't assign a new meaning to a C sy...
- d-noob (5/15) Oct 25 2009 Might be so. I don't personally like C code like
- dsimcha (11/20) Oct 25 2009 a ridiculous idea because those changes lead to code that's equal to how...
- grauzone (2/22) Oct 25 2009 D's purpose is to break C compatibility.
- Peter C. Chapin (14/18) Oct 25 2009 In C++, at least, I have often seen recommendations that say post
- Justin Johansson (13/32) Oct 25 2009 In the absence of optimization it is well known that pre-inc/dec operati...
- Lutger (7/32) Oct 25 2009 It is way too patronizing. These operators are very well known by people...
- Rainer Deyke (13/16) Oct 25 2009 There is nothing wrong with the prefix versions. '--x' is the
- Kagamin (8/16) Oct 26 2009 int PreInc(ref int i){ i++; return i; }
- bearophile (5/11) Oct 26 2009 That's a vote for my proposal then, because you have shown a way to do t...
- Kagamin (2/3) Oct 26 2009 In D you don't actually have to use them: you have ranges, foreaches etc...
- bearophile (4/5) Oct 26 2009 Very good, then restricting them will cause no problems.
This post is born from a bug I've just removed. In the past I have read more than one C coding standard (or better, lists of coding tips) that warn against bugs caused by ++ and --. They suggest to not use them compound in expressions. They allow to use them when alone on an instruction. Python designers have totally avoided to put those operators in the language, with the rationale they are bug-prone while saving just a little of typing. Removing those operators from D, as Python, may look excessive. So a possible compromise can be: - Deprecate the pre versions: --x and ++x - Make them return void, so they can't be used as expressions like this: y = x++; foo(x--); You have to use them as: x++; y = x; x--; foo(x); (So ++ and -- become similar to the Inc() and Dec() functions of Pascal). What problems such changes may cause? Bye, bearophile
Oct 25 2009
On Sun, 25 Oct 2009 12:33:13 +0200, bearophile <bearophileHUGS lycos.com> wrote:- Make them return void, so they can't be used as expressions like this: y = x++; foo(x--); You have to use them as: x++; y = x; x--; foo(x); (So ++ and -- become similar to the Inc() and Dec() functions of Pascal).No way. I've written items[itemCount++] way too often to give that up. -- Best regards, Vladimir mailto:thecybershadow gmail.com
Oct 25 2009
bearophile Wrote:This post is born from a bug I've just removed. In the past I have read more than one C coding standard (or better, lists of coding tips) that warn against bugs caused by ++ and --. They suggest to not use them compound in expressions. They allow to use them when alone on an instruction. Python designers have totally avoided to put those operators in the language, with the rationale they are bug-prone while saving just a little of typing. Removing those operators from D, as Python, may look excessive. So a possible compromise can be: - Deprecate the pre versions: --x and ++x - Make them return void, so they can't be used as expressions like this: y = x++; foo(x--); You have to use them as: x++; y = x; x--; foo(x); (So ++ and -- become similar to the Inc() and Dec() functions of Pascal). What problems such changes may cause?Apparently you want to change postInc/Dec to be statements to prevent them from returning values (otherwise the expr semantics would need a special case). That would break C compatibility and sounds ridiculous.
Oct 25 2009
d-noob:That would break C compatibility and sounds ridiculous.<It breaks C compatibility, but it doesn't assign a new meaning to a C syntax, it just disallows a syntax used in C, and this is allowed by D philosophy. Many of the things I say sound ridiculous :-) You may say it's not a handy change and you may refuse it, but I think it's not a ridiculous idea because those changes lead to code that's equal to how some expert coders suggest to program to avoid bugs, this is one of them, but I have seen two more: http://users.bestweb.net/~ctips/tip037.html So it's a language enforcement of a coding tip/standard. Bye, bearophile
Oct 25 2009
bearophile Wrote:d-noob:Might be so. I don't personally like C code like int c; c = (c++), (--c), (++c); But there has to be a middle ground. Lately there has been many attempts at removing C like parts of the grammar, but that will harm D in the long run. Once the C/C++ crowd realizes how good D is, they will only convert if there is a safe syntactical path.That would break C compatibility and sounds ridiculous.<It breaks C compatibility, but it doesn't assign a new meaning to a C syntax, it just disallows a syntax used in C, and this is allowed by D philosophy. Many of the things I say sound ridiculous :-) You may say it's not a handy change and you may refuse it, but I think it's not a ridiculous idea because those changes lead to code that's equal to how some expert coders suggest to program to avoid bugs, this is one of them, but I have seen two more: http://users.bestweb.net/~ctips/tip037.html So it's a language enforcement of a coding tip/standard.
Oct 25 2009
== Quote from bearophile (bearophileHUGS lycos.com)'s articled-noob:just disallows a syntax used in C, and this is allowed by D philosophy.That would break C compatibility and sounds ridiculous.<It breaks C compatibility, but it doesn't assign a new meaning to a C syntax, itMany of the things I say sound ridiculous :-) You may say it's not a handy change and you may refuse it, but I think it's nota ridiculous idea because those changes lead to code that's equal to how some expert coders suggest to program to avoid bugs, this is one of them, but I have seen two more:http://users.bestweb.net/~ctips/tip037.html So it's a language enforcement of a coding tip/standard.In other words, bondage and discipline. The legitimate role of a language designer for a systems language is to make it hard to shoot yourself in the foot *by accident* and to provide sane *defaults*. IMHO no language designer, not even Walter, should take it upon himself/herself enforce his/her view of "correct" or "safe" programming style on me through arbitrary restrictions on what the language can do.Bye, bearophile
Oct 25 2009
d-noob wrote:bearophile Wrote:D's purpose is to break C compatibility.This post is born from a bug I've just removed. In the past I have read more than one C coding standard (or better, lists of coding tips) that warn against bugs caused by ++ and --. They suggest to not use them compound in expressions. They allow to use them when alone on an instruction. Python designers have totally avoided to put those operators in the language, with the rationale they are bug-prone while saving just a little of typing. Removing those operators from D, as Python, may look excessive. So a possible compromise can be: - Deprecate the pre versions: --x and ++x - Make them return void, so they can't be used as expressions like this: y = x++; foo(x--); You have to use them as: x++; y = x; x--; foo(x); (So ++ and -- become similar to the Inc() and Dec() functions of Pascal). What problems such changes may cause?Apparently you want to change postInc/Dec to be statements to prevent them from returning values (otherwise the expr semantics would need a special case). That would break C compatibility and sounds ridiculous.
Oct 25 2009
bearophile <bearophileHUGS lycos.com> wrote in news:hc19h9$ih1$1 digitalmars.com:Removing those operators from D, as Python, may look excessive. So a possible compromise can be: - Deprecate the pre versions: --x and ++x - Make them return void, so they can't be used as expressions like this: y = x++;In C++, at least, I have often seen recommendations that say post increment/decrement should be avoided in favor of pre increment/decrement. The rational is that overloaded pre increment/decrement can be implemented more efficiently for complicated types---there is no need to create a temporary (you mutate the object and then return a reference to that same object). Thus I've trained myself to use pre incremenet/decrement throughout my C++ code except in cases where I explicitly need the post versions. Of course what's good for C++ does not have to be good for D. However, deprecating the pre versions might turn off some potential D converts from the C++ community. Peter
Oct 25 2009
bearophile Wrote:This post is born from a bug I've just removed. In the past I have read more than one C coding standard (or better, lists of coding tips) that warn against bugs caused by ++ and --. They suggest to not use them compound in expressions. They allow to use them when alone on an instruction. Python designers have totally avoided to put those operators in the language, with the rationale they are bug-prone while saving just a little of typing. Removing those operators from D, as Python, may look excessive. So a possible compromise can be: - Deprecate the pre versions: --x and ++x - Make them return void, so they can't be used as expressions like this: y = x++; foo(x--); You have to use them as: x++; y = x; x--; foo(x); (So ++ and -- become similar to the Inc() and Dec() functions of Pascal). What problems such changes may cause? Bye, bearophileIn the absence of optimization it is well known that pre-inc/dec operations are faster that post-inc/dec operations by simple reason of the number of memory-register-memory transfers required (at least historically this was so). Accordingly since the year dot I have always coded my for loops like so: for ( int i = 0; i < n; ++i) { ... } rather than for ( int i = 0; i < n; i++) { ... } It would be easier for me to give up red wine than the pre-inc habit. To me the proposal sounds over zealous. I don't think much of nanny-state governments either. Cheers Justin Johansson
Oct 25 2009
bearophile wrote:This post is born from a bug I've just removed. In the past I have read more than one C coding standard (or better, lists of coding tips) that warn against bugs caused by ++ and --. They suggest to not use them compound in expressions. They allow to use them when alone on an instruction. Python designers have totally avoided to put those operators in the language, with the rationale they are bug-prone while saving just a little of typing. Removing those operators from D, as Python, may look excessive. So a possible compromise can be: - Deprecate the pre versions: --x and ++x - Make them return void, so they can't be used as expressions like this: y = x++; foo(x--); You have to use them as: x++; y = x; x--; foo(x); (So ++ and -- become similar to the Inc() and Dec() functions of Pascal). What problems such changes may cause? Bye, bearophileIt is way too patronizing. These operators are very well known by people with a C background and actually quite elegant if you are used to reading them, imho.
Oct 25 2009
bearophile wrote:Removing those operators from D, as Python, may look excessive. So a possible compromise can be: - Deprecate the pre versions: --x and ++xThere is nothing wrong with the prefix versions. '--x' is the equivalent of 'x -= 1', but with less excessive typing. Any confusing expression using prefix ++/-- will be just as confusing if rewritten to use +=/-=. On the other hand, there are two things wrong with the post versions: - They use postfix notation, unlike all other unary operators in D. - They have confusion semantics. I don't use postfix ++/--. I wouldn't mind if they were removed entirely. However, the prefix versions should be kept, and allowed in compound expressions. -- Rainer Deyke - rainerd eldwood.com
Oct 25 2009
bearophile Wrote:Removing those operators from D, as Python, may look excessive. So a possible compromise can be: - Deprecate the pre versions: --x and ++x - Make them return void, so they can't be used as expressions like this: y = x++; foo(x--); You have to use them as: x++; y = x; x--; foo(x);int PreInc(ref int i){ i++; return i; } int PostInc(ref int i){ i++; return i-1; } y=PreInc(i); y=PostInc(i); just a little more difficult. y=x=0; Ever wondered what opAssign returns?
Oct 26 2009
Kagamin:int PreInc(ref int i){ i++; return i; } int PostInc(ref int i){ i++; return i-1; } y=PreInc(i); y=PostInc(i); just a little more difficult.That's a vote for my proposal then, because you have shown a way to do the same thing for people that really need to do it. In D there are often ways to do everything. To help avoid bugs there's no need to totally remove all ways to do something, but to turn them into something explicit and under programmer's control. The idea is to turn places where possible bugs can be hidden into something that can be seen. Bye, bearophile
Oct 26 2009
bearophile Wrote:Python designers have totally avoided to put those operators in the language, with the rationale they are bug-prone while saving just a little of typing.In D you don't actually have to use them: you have ranges, foreaches etc.
Oct 26 2009
Kagamin:In D you don't actually have to use them: you have ranges, foreaches etc.Very good, then restricting them will cause no problems. Bye, bearophile
Oct 26 2009
bearophile Wrote:Kagamin:He probably meant that the idea was to leave those parts of the language untouched that don't really matter to you. You can write pythonese d just like before and the rest of use can write in good old C/C++ fashion.In D you don't actually have to use them: you have ranges, foreaches etc.Very good, then restricting them will cause no problems.
Oct 26 2009
bearophile Wrote:Kagamin:Why do you use feature if you feel uncomfortable with it? If I'm not familiar with template voodoo, should I write TMP library and debug it?In D you don't actually have to use them: you have ranges, foreaches etc.Very good, then restricting them will cause no problems.
Oct 26 2009