Start learning free

LESSON 5 OF 414 · WELCOME TO SCALA

Syntax Errors in Scala: Reading Compiler Messages

Lesson Read the full explanation

TL ; DR:

  • Syntax errors are mistakes in the use of the Scala language, analogous to spelling or grammar mistakes in a language like English. For example:


  • Omitting quotes when working with strings:

    println("Welcome!)
    

  • Omitting parentheses when using the println function:

    println("Let's code!"
    

  • When Scala encounters a syntax error, it reports a compile error and your program does not run at all.




Full lesson:

Humans are prone to making mistakes. Humans are also typically in charge of creating computer programs. To compensate, programming languages attempt to understand and explain mistakes made in their programs.

Here are two common errors that we encounter while writing Scala code:


The syntax error:

Syntax errors are mistakes in the use of the Scala language, and are analogous to spelling or grammar mistakes in a language like English. For example, the sentence "Would you some tea?" does not make sense – it is missing a verb.

Syntax error means there is something wrong with the way your program is written. Some examples:

1. Mismatched quotes when declaring a string:

var greeting = 'Hey there!"

When we run this code we'll get an error such as unclosed string literal.

2. Leaving out a parenthesis:

println"Hey")

When we run this code we'll get a syntax error, because the closing parenthesis has no opening one.


The reference error:

A reference error occurs when the Scala compiler sees a word it does not recognize. Some examples:

1. Misspelling a class / function name:

prntln("Hey")

When we run this code we'll get the error Not found: prntln.

2. Using a non-existent variable / Misspelling a variable's name:

var animal = "parrot"
println(aniimal)

When we run this code we'll get the error Not found: aniimal.


Assignment
Follow the Coding Tutorial and let's practice with errors!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    SCALA · WELCOME TO SCALA

    Syntax Errors in Scala, explained

    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

    1. Read the line it points to. The message includes the offending line and a marker under the place where it got confused.
    2. Read the short description. Words like unclosed, expected and Not found tell you what kind of problem it is.
    3. Check the line above. The real mistake is sometimes just before the reported position.
    4. 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 found message 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.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free