3 ms·
> why do C-like languages have so many ways to syntactically group things?) TL;DR math notation Because they are different things. Brackets are for types, cur
by ithkuil 6y ago
> why do C-like languages have so many ways to syntactically group things?)
TL;DR math notation
Because they are different things. Brackets are for types, curly braces are for "bodies" (struct definitions, initializers, statements, function bodies, control flow construct bodies), parentheses are for overriding precedence.
There are obviously exceptions that don't fit in this general scheme: e.g. function call syntax is based on parentheses, so it's not just about precedence.
All this is just a post-fact rationalization; this all comes from standard math notation.
Math uses parentheses for function application. Math uses commas for arguments of functions of multiple variables. Math uses curly braces to state of things are defined (sets, inductive rules) etc. Math uses parentheses to override precedence.
All the rest is that historical accident. Angle brackets were unused so they choose them for types. Square brackets for array indexing? No idea why
- deleted 6y ago[deleted]
- dan-robertson 6y agoThis is not an accurate description of notation as it is used in mathematics. All types of brackets (round, curly, square, angled, ...) are used in mathematics for grouping things. Typically parens for innermost small groups and other brackets for larger “outer” groups. Curly brackets may be used for sets but they are also used for different cases of functions, or as a more succinct and precise way of expressing sentences about multiple things than writing “foo, respectively bar” all over the place, or for Stirling numbers of the second kind, or for whatever other things an author might want. Other “structure making” notations like group presentations use angle brackets. Ideals are sometimes written with their generators in angle brackets, sometimes with round brackets. Similarly tuples or sequences may be written with either. Matrices are sometimes written with square brackets and sometimes round, and never with commas. Vectors typically are written with round brackets but may be square. Row vectors sometimes have commas. Function application is sometimes written with round brackets. Arguments are sometimes separated by commas, sometimes by semicolons, sometimes with one argument as a subscript or implicit. Application often omits brackets altogether. Sometimes the function goes on the left, sometimes it goes on the right. Now consider the few features which I find are relatively common to different mathematics notations are context, alignment and structure in 2 dimensions, juxtaposition, implicitness, and terseness. I think I see basically none of these in the common C/Rust style syntax you claim to be so rooted in mathematical notation, and indeed I would guess that you would oppose them. I don’t buy your arguments at all.
- ithkuil 6y agoThere is another ingredient I failed to make explicit: c syntax was a product of the 70s and it was a tradeoff between writing a parser easily and designing a syntax that would feel reasonably natural to people used to other notations. Surely implicitness and tersness were wisely used in math notations then and now, but devising a formal syntax that would work for C at the time was likely deemed too much work for little gain. there is a lot of random decisions that shaped the C syntax, as it's ultimately a random even that so many modern languages have chosen to continue that trend. My point was more about why it's not completely irrational to use "many different grouping symbols" since math notation also uses many different grouping symbols (rather than arguing the exact reasons this or that symbol is used instead of something else)
- dboreham 6y agoSquare brackets because they look like matrices perhaps?