Closed anatoliykmetyuk closed 9 years ago
Also, roughly same thing happens when trying to evaluate a mathematical expression in a script variable definition:
val i = 2 * 2
This is because in the current syntax specification the RHS of a local variable initializer is a simpleValueExpression:
simpleValueExpression = "_"
+ literal
+ "new" (classTemplate + templateBody)
+ ( "here"
+ currentInstanceExpression
+ identifier . "." currentInstanceExpression )
(.. "." identifier)
So operator symbols such as -
and *
are not allowed.
But maybe we should change this, just like newlines may have to become a different kind of whitespace than spaces and tabs. A val/var declaration is always an operand of a sequence operator, and not the last operand there. Maybe such a declaration should be terminated by a semicolon or newline, so that the RHS may become any Scala value expression.
A var/val initializer expression may require that an annotation directive be run first. Then this annotation should be placed just before the expression, rather than before the var/val keyword. E.g.,
val selectionResult = @gui: fileChooser.showSaveDialog(destinationFileLabel)
This way it will be easier to enforce that after the declaration a sequential operator follows.
Solved
Consider the following script:
On compilation, it will fail the compiler with the following exception (first few lines):
If we rewrite it without specifying the type, like this:
then before the exception we'll get the following error:
Positive integers work correctly.
A workaround to the problem is to enclose the negative integer into parentheses: