-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Remove the requirement of () for if, while, for, switch, etc. #1659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
What about if expressions returning values?
|
This looks confusing to me no matter if parens/braces are omitted or not because there are so many levels of branching on a single line.
Personally I think it's a good thing to enforce fewer parens on the user. Whether the code becomes more or less readable comes down to what the reader is used to. In the end it comes down to personal preferences again. However, what about non-human readers? Do the parens help the compiler to emit better error messages? Is it easier for tools to parse and understand the code with enforced braces? I imagine dropping parens here will make it harder for text editors to trivially detect the code structure (I'm thinking about sublime text 3's way of defining syntax via regex, not sure how other editors do it). |
Status quo
Alternative - Syntax more consistent with switch
Cons:
Some languages when construct that acts like a chain of if/elseifs:
This goes hand-in-hand with something to do with the return type ambiguity issue. I've mentioned before but never got around to a proposal: Function expressions!
|
this is ambiguous: const b: bool = if a - b == c -d -c == -d else - a - b;
const b: bool = if (a - b == c -d) -c == -d else - a - b; |
I think |
@Meai1 I do agree with your rationale and premise however I don't think we can do that here since that would have the effect of making zig's grammar whitespace sensitive which I don't think we want. |
Not a 100% sold on this syntax, but it is optional so all ambiguous code is solved with parens. Its really an infix issue, so lets just switch to postfix! if a b - c d - == -c -d == else -a -b == |
And yes the ambiguous code even more noticeable in the postfix notation. |
allow the usage of then for expressions x = if c then a else b;
x = if (if a then b else c) then d else e; for statements, well, {} are clear enough |
usage of {} should remain consistent across for/while/if if a {stuff;} else {other_stuff;}
while a {stuff;}
for all_your_base |r| {r.belongToUs();} no idea for a proper switch syntax :( |
Without requiring parentheses around the condition of this: return if a - b else c;
parses as: return if (a - b) else c;
and this: return if (a) -b else c;
parses as: return if ((a) - b) else c; I think this issue is a bad idea. We need a way to unambiguously signal the end of the |
yes we can, if we remove the unnecessary ability to put multiple statements on the same line with an if
well I cant even type tabs without the zig compiler throwing errors, I'm sure people dont mind a sensible restriction that prevents you from stuffing several nested if conditions into a single line. Golang has that too, it doesnt allow an if without {} braces. |
wait, is it only prefix symbols that are causing this issue? |
x = if if true true else false 1 + 2 else 3; This isn't ambiguous, its bad code. You never nest turnary statements, so why would you nest an if statement in the conditional? If we want to avoid this foot gun then we should not allow an if statement in a conditional. We could even require that nested ifs must be in |
Remember that blocks in Go abolished ternary operators altogether(link) because "the language's designers had seen the operation used too often to create impenetrably complex expressions.". I have not suffered from this, but it is an interesting argument. I'd like to see how that rule plays out in real actual Go code that would otherwise use ternary operators.
The ambiguity comes from tokens that can either be between two primary expressions or come before a primary expression. |
Regarding blocks with braces, there's still #732 for the new result keyword.
That's not so bad.
|
With #1628 solving #760, the parens around expressions in if, while, for, switch, etc. should not be necessary anymore.
The text was updated successfully, but these errors were encountered: