Optional braces

bearophile bearophileHUGS at lycos.com
Sun Aug 21 20:35:20 PDT 2011


Jonathan M Davis:

> Except that it's not a bug. It's an issue that crops up in grammars for 
> programming languages.

Right, I have just seen I was wrong :-)


> There is classic solution to the problem which D 
> follows. So, there is no ambiguity, and there is no bug. It's very much by 
> design.

There is no ambiguity for the compiler, because in C/C++/D the else is associated to the nearest if. But programmers are mammals, they aren't compilers. So they sometimes err, despite there is no formal ambiguity.


>  Bugs caused by mis-handling if statements related to dangling elses are 
> _very_ rare in C-based languages. I really think that the only reason that 
> you're running into this at all is because you've been translating python code 
> to D without adding braces.

It's not a very common bug, but in past it has happened to me not just in translating Python code. Ada, Python, Pascal and other languages have specific means to avoid this bug. It's a bug easy to avoid, and style guides often suggest to add braces in this situation anyway.

See also for more practical considerations:
http://code.google.com/p/copute/issues/detail?id=21

Bye,
bearophile


More information about the Digitalmars-d mailing list