4 ms·
Not that dumb as one might think. What is the purpose of the opening brace after a function declaration in C, for example? It's the only token accepted, so its
by sph 3y ago
Not that dumb as one might think. What is the purpose of the opening brace after a function declaration in C, for example? It's the only token accepted, so its sole purpose is symmetry with the closing brace, which has an actual purpose.
So it might look silly, but there is no deep reason it couldn't have just been
int main()
printf("hello");
}
That's just as unambiguous to parse.
- vore 3y agoNot in the original C, where you had to declare function parameter types like so: int foo(x) int x; { return x; } and not in C23 (or GNU C) either, where this is legal (this example is not super useful): int foo(int x) [[gnu::aligned]] { return x; }
- mikepavone 3y agoTechnically, the opening brace is needed in C to support K&R style function declarations which apparently were still legal in C11 at least, but I doubt that's a feature anyone wants to replicate in a new language.
- layer8 3y agoIn that case, I would prefer Pascal’s (and Basic’s) “end”, however.
- zarzavat 3y agoUnmatched braces completely mess up text editors, which would need to include a full parser in order to pair-match brackets (it also creates an unresolved tension that will stay with you all day :)
- goku12 3y agoI was going to say the same. And yes - it causes OCD in 90% of devs out there. But it also feels somewhat wrong for editors to match brackets to infer block hierarchy. What if blocks have some sane rule that we don't know yet (i.e other than bracketed blocks and indented blocks)? Perhaps treesitter is the one-stop solution for that problem.
- cozzyd 3y agoIndeed. Things like di{ probably don't work...
- kgwxd 3y ago> :) That’s a mouth, not a paren :/ I won’t be able to sleep tonight :)) (that’s better
- The_Colonel 3y agoMany languages are designed with IDEs in mind and "dumb" text editors are not a concern.
- vidarh 3y agoText editors need fairly complete parsers to match braces anyway. E.g. consider even something as simple as { "}" }. Or {"\"}. The former is reasonably unambiguous, but the latter requires knowing if the escape style of double quoted strings in the given language escapes the second '"' or not. Now try proc {"#{"}"}"} (valid Ruby) vs proc {"{\#{"}"}"} (invalid)
- norir 3y agoI have an even better idea: main(): int printf "hello" 0 ;)
- lambda_garden 3y agoTrouble with that is invoking main and declaring main are too similar. I prefer this: let main () = printf "hello" 0
- sltkr 3y agoWhy do we put spaces after punctuation in English?It's completely redundant.actually,why even put a capital letter at the beginning of a sentence?it can obviously be inferred. OK, I'll stop now, but the point is: matching braces provide visual structure that's helpful to read code, even if it's syntactically redundant.
- sph 3y agoProse is not a programming language, isn't it obvious? One is designed for machines to parse unambiguously, the other has grown organically and chaotically over hundreds of years. Machine parsing has never been a design concern.
- frodowtf 3y agoIndentation provides visual structure. Matching braces hardly add value to it.
- Q6T46nT668w6i3m 3y agoYou identified its purpose: symmetry.