Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>All this talk about recursive function and type is a red-herring. Scala's grammar does not allow for postfix operators in any other place than the end of an expression. This is the grammar we are talking about: the syntax of things without any semantics. Here is the relevant grammar from the specs:</p> <pre><code>Expr ::= (Bindings | [‘implicit’] id | ‘_’) ‘=&gt;’ Expr | Expr1 Expr1 ::= ‘if’ ‘(’ Expr ‘)’ {nl} Expr [[semi] else Expr] | ‘while’ ‘(’ Expr ‘)’ {nl} Expr | ‘try’ ‘{’ Block ‘}’ [‘catch’ ‘{’ CaseClauses ‘}’] [‘finally’ Expr] | ‘do’ Expr [semi] ‘while’ ‘(’ Expr ’)’ | ‘for’ (‘(’ Enumerators ‘)’ | ‘{’ Enumerators ‘}’) | {nl} [‘yield’] Expr | ‘throw’ Expr | ‘return’ [Expr] | [SimpleExpr ‘.’] id ‘=’ Expr | SimpleExpr1 ArgumentExprs ‘=’ Expr | PostfixExpr | PostfixExpr Ascription | PostfixExpr ‘match’ ‘{’ CaseClauses ‘}’ PostfixExpr ::= InfixExpr [id [nl]] </code></pre> <p>The only two places where <code>PostfixExpr</code> appears beside these are after the <code>if</code> on a <code>case</code> statement and before <code>: _*</code> on an argument list. So, looking at that, we see that the only things that can appear on the right side of a postfix expression's method name is a type ascription or a <code>match</code>.</p> <p>So, what end expressions? Well, expressions appears in a lot of places in the grammar, so there's a lot of things that could end it. In this particular example, the expression is a <code>BlockStat</code> inside a <code>Block</code>, so it must end with a semi-colon, which may be inferred or not.</p> <p>To infer this semi-colon, it is necessary that the next line must not be something that could be parsed as another kind of expression. In this particular case, we have this:</p> <pre><code> val aFact = a! println("factorial of " + a + " is " + aFact) </code></pre> <p>Now, let's rewrite that from the point of view of the compiler:</p> <pre><code> val id = id id id ( stringLit id id id stringLit id id ) </code></pre> <p>These literals and identifiers are parsed like this:</p> <pre><code> val id = id id id ( expr ) val Pattern2 = SimpleExpr1 id SimpleExpr1 ArgumentExprs val Pattern2 = InfixExpr val Pattern2 = Expr val PatDef PatVarDef Def BlockStat </code></pre> <p>So that looks like a valid infix expression to the compiler as he parsed your program. Afterwards, it noticed the types didn't match, but it's too late to go back and see if a semi-colon could be inferred.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload