In making an analogy between smart quotes and programming languages, it occurred to me that distinct characters for opening and closing delimiters might not be necessary, but simply a choice in readability.
For example, arguments in Ruby’s anonymous functions use identical pipes to open and close. Haskell uses white space with extreme prejudice.
I am not asking if different types of delimiters are necessary — brackets for indexers, braces for blocks — but whether distinct open and close braces (e.g. (
and )
) are syntactically necessary in most languages, or simply a preference of the designers.
Not syntactically necessary, but if open and close delimiters are the same, it makes it difficult (or impossible) to nest things. Exhibit A is the POSIX shell, where
var=`command`
was replace with
var=$(command)
precisely because the code with identical opening and closing delimiters does not nest.
Having distinct delimiters allows nesting. Ruby's block parameter list does not support nesting, so using the same delimiter is okay.
In C and C++, bare braces can be nested, and open nested lexical scopes:
{
int a = 42;
{
int a = 24;
{
printf("%d\n", a); // prints 24
}
}
}
Use identical delimeters and this is ambiguous:
|
int a = 42;
|
int a = 24;
| // Is this an open or close pipe?
printf("%d\n", a); // 24? 42?
| // could be a pair
| // of open+close
|
Given the prevalence of C syntax rules, there are likely to be many other languages with similar issues (perl, for one).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With