Parenthesis around if/for/while condition is not necessary

aliak something at something.com
Sun Jun 24 23:08:15 UTC 2018


On Sunday, 24 June 2018 at 11:27:12 UTC, Basile B. wrote:
> On Saturday, 23 June 2018 at 06:24:29 UTC, Basile B. wrote:
>> On Saturday, 23 June 2018 at 06:18:53 UTC, user1234 wrote:
>>> On Saturday, 23 June 2018 at 05:09:13 UTC, aedt wrote:
>>>> On Saturday, 23 June 2018 at 04:45:07 UTC, user1234 wrote:
>>>>> [...]
>>>>
>>>> Same thing as the following"
>>>> return a && b;
>>>>
>>>> I'm not saying to drop parens completely, I'm saying why is 
>>>> it not optional. D seems to have no problem with x.to!string 
>>>> or std.lines
>>>
>>> I agree that this would be in adequation with certain stuff 
>>> of the D syntax, such as parens-less single template 
>>> parameter. Someone has to make a DIP for this
>>
>> Maybe but this is a simple parser thing. For example after 
>> reading the discussion here i have tested the idea in my toy 
>> programming language 
>> (https://github.com/BBasile/styx/commit/83c96d8a789aa82f9bed254ab342ffc4aed4fd88) and i believe that for D this would be as simple ( < 20 SLOC, w/o the tests).
>>
>>> otherwise we're good for one of this sterile NG discussion 
>>> leading to nothing, i.e intellectual mast... well guess the 
>>> word.
>>
>> I'm tempted to try this in DMDFE. Change is simple enough so 
>> that if it get rejected no much time is lost.
>
> FYI this works fine, as expected it's just some small parser 
> changes.
> I didn't touch to for and foreach for now. I think that 
> SwitchStatement is a candidate too.
>
> https://github.com/BBasile/dmd/commit/5455a65c8fdee5a6d198782d1f168906b59e6d3d
>
> However note that there's a nice thing with the phobos style 
> that won't be that nice anymore:
>
> if (condition)
>     action();
> ----^
>
> if condition)
>     action();
> ---^
> ----^
>
> It's not nicely aligned anymore !

Wow nice, that was quick, would it be much more to make it so 
that braces are required with if statements that do not start 
with an open paren? Then you avoid the ambiguity I guess ... It'd 
be awesome if D allowed it optionally though.

Not that I have a strong opinion here but I have found that being 
able to omit the parens makes the code look A Lot cleaner. And 
forcing braces I've also come to apprecaite with a bonus of 
avoiding this bug:

if (a)
   b;
   c;

Though forcing braces in D may make phobos quite sparse since it 
uses allman braces so we'll get 4 line if statements everywhere :p

Cheers,
- Ali




More information about the Digitalmars-d mailing list