Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are distinct open and close delimiters syntactically necessary?

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.

like image 842
Matt Sherman Avatar asked Jul 01 '11 01:07

Matt Sherman


3 Answers

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.

like image 183
Norman Ramsey Avatar answered Sep 22 '22 08:09

Norman Ramsey


Having distinct delimiters allows nesting. Ruby's block parameter list does not support nesting, so using the same delimiter is okay.

like image 36
Chris Jester-Young Avatar answered Sep 24 '22 08:09

Chris Jester-Young


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).

like image 22
bdonlan Avatar answered Sep 23 '22 08:09

bdonlan