Draco-lang / Language-suggestions

Collecting ideas for a new .NET language that could replace C#
75 stars 5 forks source link

Separator for conditions, loop-members, blocks #25

Closed LPeter1997 closed 2 years ago

LPeter1997 commented 2 years ago

There are a few different styles we could pick when designing the syntax for the basic control flow structures. The options are in no particular order.

Option 1: Required parentheses for the condition element

This is how C does it. Allows the parser to distinguish the condition from the block, if the block braces are omitted.

if (x) y();
if (x) y(); else z();
if (x) { y(); z(); }
for (i in list) print(i);
for (i in list) { print(i); log(i + 1); }

Option 2: Required braces for the block element

This "frees up" the condition, but for separability the block always has to be explicit.

if x { y(); }
if x { y(); } else { z(); }
if x { y(); z(); }
for i in list { print(i); }
for i in list { print(i); log(i + 1); }

Option 3: Separator keywords

Separator keywords like then and do reduce the number of separator tokens, as they don't need to be symmetric like parentheses.

if x then y();
if x then y(); else z();
if x then { y(); z(); }
for i in list do print(i);
for i in list do { print(i); log(i + 1); }

Note that the block syntax and semicolon usage are still up for discussion. Personally I really like option 3 so far, even with the current syntax. { } can be thought of as the block composing operator or something.

yamin8000 commented 2 years ago

I think Option 3 just adds more unnecessary noise to the code. I mean it's obvious when I write:

if (foo() == bar()) {
      baz()
}

That means I want to call baz method only and only if foo() == bar() condition is met, any other keywords like do or then are just redundant or even noise. It may make code more readable for non-programmers but it would make code less concise and more verbose in the long run.

What is important is as @LPeter1997 said:

but for separability, the block always has to be explicit

We're always going to need some sort of characters like { , } to define blocks. In conclusion I prefer Option 1.