2

Just curious, from a syntax parsing perspective, why does Scala actually require parentheses around the condition of an if statement, given that the only thing allowed after if is (?

  • Because it has inherited that (indirectly) from C (via Java probably). – Basile Starynkevitch Jul 20 '15 at 8:02
7

The parentheses don't only delimit where the condition starts; they also indicate where it ends.

You're right that technically, only the right parenthesis is necessary to resolve ambiguity. But language designers are aesthetically motivated, and having only one of a matched pair of characters would look just awful. There are languages which delimit the end but not the start of a condition, e.g. Bash, but no one has ever accused them of looking good.

  • Right of course. :) – matt Jul 20 '15 at 8:13
  • 2
    A notable example of asymmetric if is Python, with its affinity to lines, indentation and colons. – Lars Viklund Jul 20 '15 at 9:25
  • 1
    @LarsViklund Haskell is another example: if a == b then e1 else e2. – jcora Jul 20 '15 at 9:38
6

Scala is designed not only to be interoperable with its host platform technically, but also socially, i.e. it is designed to be familiar to existing programmers of the host platform. And on almost all the host platforms that the Scala community could potentially be interested in (JVM, CLI, ECMAScript, Objective-C/Core Foundation/Cocoa, Unix, Windows), the dominant language(s) (Java, C#, ECMAScript, Objective-C, C, C++) use this syntax. That's why Martin Odersky chose it: familiarity.

However, when Martin Odersky was asked what the one thing was that he introduced for compatibility with Java and that he would like to change, he mentioned exactly this syntax.

This asymmetry of having an if keyword to indicate the condition, an else keyword to indicate the else branch, but no corresponding then keyword to indicate the then branch needlessly complicates the syntax, and he wishes that he had chosen to use a then keyword instead. Unfortunately, it is impossible to introduce this in a backwards-compatible manner, so it will likely stay the way it is forever.

  • He being?! anyway, I concur, if I had to list the number of times I forgot the parentheses, and it makes code less friendly to read. I guess in scala => would come to mind as a more natural choice. – matt Jul 20 '15 at 13:00
  • Sorry, "he" is Martin Odersky, the designer of Scala (and Java Generics, and the author of Oracle JDK's/OpenJDK's javac). – Jörg W Mittag Jul 20 '15 at 13:15
  • 1
    There is SIP12 which is resolving this problem. I guess it comes former or later. – kiritsuku Jul 20 '15 at 15:59
1

Blame the designers of B!

The choice to use a pair of parentheses to delimit conditions is not specific to Scala, but is common to all languages based on C-syntax, including Java, C++, C# and even JavaScript. Those languages are based on C-syntax simply because it is familiar to many developers. And C inherited this syntax from its predecessor B (which like C was designed by Ken Thompson).

A language like Lisp does not require delimiters around conditions:

(if x do-something do-something-else)

But this is only possible because of the regularity of the Lisp-syntax. In a language with in infix-operators, this will lead to ambiguities, because

if a ++ b;

could mean either

if (a) ++b;

or

if (a++) b;

So, while the opening parenthesis after if is theoretically not necessary, you do need some token to separate the end of the condition from the following statement.

Python for example uses a colon for this purpose:

if a == b: 
   do_something()

A lot of languages, like Pascal, Basic, OCaml, F#, Haskell uses the keyword then:

if a == b then do_something

So the question comes down to why the designers of B decided to use a set of parenthesis to delimit conditions? BCPL, the predecessor to B, uses the keyword 'do', so it was an active choice by the designers of B to use parenthesis instead.

I can't answer that (only the designers of B can), but a reasonable assumption is they preferred to use special characters rather than keywords (perhaps to make it quicker to type and keep the number of reserved keywords low), and since parentheses are already used to delimit sub-expressions, it seem like a logical choice.

  • 1
    Apparently, it was already in B, C's predecessor: en.wikipedia.org/wiki/B_(programming_language)#Examples – Jörg W Mittag Jul 20 '15 at 13:39
  • @JörgWMittag: Thanks for that info, I updated the answer. – JacquesB Jul 20 '15 at 13:54
  • Jörg W Mittag points out in his answer that the Scala designer consciously copied this from C (and everyone else who copied it from C). So there were three decisions, the decision by the designers of B to do it this way, and the decision to copy this from B into C, and the decision to copy from C into Scala. – John R. Strohm Jul 20 '15 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.