[Issue 4375] Require explicit braces when 'else' is ambiguous

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Sep 23 06:00:31 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4375


Stewart Gordon <smjg at iname.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg at iname.com


--- Comment #6 from Stewart Gordon <smjg at iname.com> 2010-09-23 05:59:44 PDT ---
(In reply to comment #1)
> Except that then you have to make the compiler understand 
> indentation.

???  Defining the form

    if ( IfCondition ) if ( IfCondition ) ThenStatement else ElseStatement

to be an error, rather than to be parsed in one of the two possible 
ways, doesn't require the compiler to understand indentation at all.

> Requiring it to be able to understand indentation well enough to 
> alert the programmer as to where they used it incorrectly or to 
> complain that the programmer has used it in a manner which could be 
> ambiguous to a human reader would likely complicate things quite a 
> bit for the compiler.

But if you design the language to be whitespace- and indent-sensitive 
in the first place, then it's easy.  That's how Python has managed 
it.  You need a different type of parser to read the language, but it 
isn't difficult.

On the other hand, you can't add indent-sensitivity to a free-form 
language, unless you discard the free-form aspect and require line 
breaks in set places.  But then it would be silly to require curly 
brackets and semicolons, which is why Python doesn't.  This would be 
a huge breaking change for D were it to be implemented.

> Also, it could be rather nasty to code generators, since then 
> they'd have to worry a lot more about making code human-readable - 
> which while potentially nice probably isn't necessary in many cases.

ISTM code generators would likely put the {} in, because it's easier 
to program it to always put them in than to detect whether they're 
necessary.

> I believe that the usual solution if a programmer is worried about 
> this sort of thing is just to use a coding standard that always 
> requires braces for the bodies of if-else statements, loops, etc.

My personal style is to always use {} unless the whole IfStatement or 
similar fits on one line.  Though I do use "else if" rather than

else {
    if (...) {
        ...
    }
}

and I've been known to do things like

else switch (...) {
    ...
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list