I’m with you on this, but we’re unlikely to get it. There are some basic aspects of the syntax which come down to Andrew’s opinion on what is the clearest expression in terms of reading and understanding code. Parentheses around control-flow statements is one of those. I agree that they do a good job of visually separating what’s going on, I’m not as convinced that it’s actually important. What’s readable is inherently subjective, which always makes syntax questions a rich substrate for bikeshedding.
If I were Syntax Czar the language would look pretty different:
- Optional semicolons
- Mandatory braces on
if
/else
, except on a single line† - Optional parentheses for
if
andfor
, also optional for a one-clausewhile
, but not two-clause: sowhile i < thing.len {...}
is legal,while (i < thing.len) : (i += 1) {...}
is required.
Thing is, I don’t think that the status quo has made any bad decisions. The longer I use the language, the less convinced I am that making semicolons optional would constitute an improvement. It would make things cleaner, in my subjective aesthetic judgement, but not necessarily clearer, and I do agree that the latter is most important.
Mostly it just doesn’t matter much, I’ve used enough languages that syntax is largely background noise. Unless it’s getting in my way, which Zig’s basically doesn’t. Although see footnote.
† if
assignments would use something which I think the language should have anyway, which is that assigning if
can use break
(but statement if
cannot).
const a = if pred {
break 5;
} else {
break 7;
}
Or just add the parens back for a real proposal, because while I would prefer the ability to drop parens, it’s not something I’d go to bat for.
I not-infrequently do an awkward dance to get around this lack already:
const a = a_val: {
if (predicate) {
const intermediate = aThingIHaveToDoFirst();
break :a_val outer_var + intermediate;
} else {
break :a_val outer_var;
}
};
The extra layer of scope and the label aren’t really bringing much to the table, they’re just letting me both do something in an if
statement and return a value from it.
That also means that there’s a lot of extraneous work involved in converting a simple assignment if
to this form: I have to add the braces (which aren’t allowed in the assignment form), wrap it in a scope, think up a label, and turn both prongs of the if
into break
statements.