What a syntax error is
People make mistakes when they write code, so Scala checks a program before running it and explains what it doesn't understand. A syntax error is a mistake in how the code is written, like a spelling or grammar mistake in English. The sentence "Would you some tea?" is missing a verb, and a reader can't make sense of it. In the same way, the compiler can't make sense of code that breaks the language's rules.
Scala is a compiled language, so the whole program is checked first. When the compiler finds a syntax error, it prints an error message and the program does not run at all. Nothing before the mistake is executed either.
Typical syntax errors
A missing closing quote
println("Welcome!)
The string starts at the opening quote but never ends. The compiler reports unclosed string literal and points at the opening quote. The fix is to close the string: println("Welcome!").
Parentheses that don't match
println("Hello!";
The call is opened with ( but never closed. Here the compiler stops at the semicolon and reports ')' expected, but ';' found. The message names the symbol it wanted and the one it saw instead. The fix is println("Hello!").
When a closing parenthesis is missing, the compiler often notices only at a later point, so the position it reports can be after the real mistake. Look at the line it points to and at the lines just above it.
Wrong quotes
Text uses double quotes. Starting a string with a single quote and ending it with a double quote, as in 'Hey there!", confuses the compiler, which reads a single quote as the start of something else, and it reports errors on that line. Use matching double quotes: "Hey there!".
Reference errors
A second kind of mistake is code that is well formed but uses a name Scala doesn't know. The compiler reports it as Not found, and Scala often suggests the name you probably meant:
prntln("Hey")
Not found: prntln - did you mean println?
The same happens with a misspelled variable:
var animal = "parrot"
println(aniimal)
Not found: aniimal - did you mean animal?
Names are case-sensitive, so Animal and animal are different names.
Type errors
A third kind is a value of the wrong type. val age: Int = "30" fails because the code promises an Int but supplies a String. The message says what was found and what was required.
How to read a compiler message
- Read the line it points to. The message includes the offending line and a marker under the place where it got confused.
- Read the short description. Words like
unclosed,expectedandNot foundtell you what kind of problem it is. - Check the line above. The real mistake is sometimes just before the reported position.
- Fix one error at a time and compile again. One mistake can cause several messages, and fixing it removes them all.
The exercise
The starting code has two println lines, each with one mistake, separated by a blank line:
println("Hello!";
println("I love coding!)
The first call is missing its closing parenthesis (a semicolon sits in its place), and the string in the second call is missing its closing quote. Fix each one, and the program prints Hello! and I love coding!:
println("Hello!")
println("I love coding!")
Key takeaways
- A syntax error is code that breaks the language's rules, and the program doesn't run until it is fixed.
- Every opening quote and parenthesis needs a closing partner.
- A
Not foundmessage means a misspelled or unknown name. - Read the message, look at the marked line and the one above it, and fix errors one at a time.
