www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: Treating the abusive unsigned syndrome

reply Kagamin <spam here.lot> writes:
bearophile Wrote:

 One solution is to "disable" some of the more error-prone syntax allowed in C,
turning it into a compilation error. For example I have seen newbies write bugs
caused by leaving & where a && was necessary. In such case just adopting "and"
and making "&&" a syntax error solves the problem and doesn't lead to bugs when
you convert C code to D (you just use a search&replace, replacing && with and
on the code).

Why do you want to turn D into Python? You already has one. Just write in python, migrate others to it and be done with C family.
Nov 26 2008
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
bearophile:
 One solution is to "disable" some of the more error-prone syntax allowed in C,
turning it into a compilation error. For example I have seen newbies write bugs
caused by leaving & where a && was necessary. In such case just adopting "and"
and making "&&" a syntax error solves the problem and doesn't lead to bugs when
you convert C code to D (you just use a search&replace, replacing && with and
on the code).<<


Kagamin:
Why do you want to turn D into Python? You already has one. Just write in
python, migrate others to it and be done with C family.<

The mistake I have shown of using "&&" instead of "&" or vice-versa, and "|" instead of "||" and vice-versa comes from code I have seen written by new programmers at he University. But no only newbies can put such bugs, see for example this post: http://gcc.gnu.org/ml/gcc-patches/2004-10/msg00990.html It says:
People sometimes code "a && MASK" when they intended "a & MASK". gcc itself
does not seem to have examples of this, here are some in the linux-2.4.20
kernel:<

I want to copy the syntax that leads to less bugs and more readability, and often Python gives good examples, because it's often well designed. Note that this change doesn't lead to less performance of D code. Also note that G++ already allows you to write programs with and, or, not, xor, etc. The following code compiles and run correctly, so instead of Python you may also say I want to copy G++: #include "stdio.h" #include "stdlib.h" int main(int argc, char** argv) { int b1 = argc >= 2 ? atoi(argv[1]) : 0; int b2 = argc >= 3 ? atoi(argv[2]) : 0; printf("%d\n", b1 and b2); return 0; } That can be disabled with "-fno-operator-names" while the "-foperator-names" is enabled by default. So maybe the G++ designers agree with me, instead of you. Bye, bearophile
Nov 26 2008
next sibling parent Kagamin <spam here.lot> writes:
bearophile Wrote:

 Also note that G++ already allows you to write programs with and, or, not,
xor, etc. The following code compiles and run correctly, so instead of Python
you may also say I want to copy G++:

copying G++ is not always a good idea :) As I remember this alternative syntax is supported for compatibility with keyboards which don't have kinda exotic ~^&| characters. And I don't think that there is a method to make && a syntax error as you proposed.
Nov 26 2008
prev sibling parent reply Kagamin <spam here.lot> writes:
bearophile Wrote:

 http://gcc.gnu.org/ml/gcc-patches/2004-10/msg00990.html
 It says:
People sometimes code "a && MASK" when they intended "a & MASK". gcc itself
does not seem to have examples of this, here are some in the linux-2.4.20
kernel:<


that thread is about an extra compiler warning (which is always good), not about breaking C syntax.
Nov 26 2008
parent bearophile <bearophileHUGS lycos.com> writes:
Kagamin:
that thread is about an extra compiler warning (which is always good), not
about breaking C syntax.<

You seem unaware of the current stance of Walter towards warnings. And please don't forget that D purposes are different from C ones (D is designed to be safer, especially if this has little or no costs), and that D comes after a long experience of coding in C, and that D runs on machine thousands of times faster than the original ones the C language was designed for (today having fast kernels in your program is more and more important. Less code uses most of the running time). And that thread was more generally an example that shows why that specific C syntax is error-prone, and it also explains why some languages, among them there's Python too but it's not the only one, have refused this specific C syntax. Note that there are several other C syntaxes/semantics that are error-prone, and thanks Walter D already fixes some of them, and I hope to see more improvements in the future.
And I don't think that there is a method to make && a syntax error as you
proposed.<

Keeping two syntaxes to do the same thing is a bad form of complexity. Generally it's better to have only one obvious way to do something :-) Bye, bearophile
Nov 26 2008
prev sibling parent "Nick Sabalausky" <a a.a> writes:
"Kagamin" <spam here.lot> wrote in message 
news:ggjcfg$fqq$1 digitalmars.com...
 bearophile Wrote:

 One solution is to "disable" some of the more error-prone syntax allowed 
 in C, turning it into a compilation error. For example I have seen 
 newbies write bugs caused by leaving & where a && was necessary. In such 
 case just adopting "and" and making "&&" a syntax error solves the 
 problem and doesn't lead to bugs when you convert C code to D (you just 
 use a search&replace, replacing && with and on the code).

Why do you want to turn D into Python? You already has one. Just write in python, migrate others to it and be done with C family.

Python has other issues.
Nov 26 2008