Common errors
The compiler's messages are the language's documentation at the moment it matters. The five below are the ones a beginner meets first, each with the cause and the fix. Every program is compiled on its own with scalac; a class folder is created once, and each command writes the status of the compiler after its diagnostics.
mkdir -p out
A type that does not match
Scala infers the type of a value from its expression and checks the declared type against it. A string is not a number, and the message names both sides and the expression it found:
type_mismatch.scala
object TypeMismatch:
val count: Int = "three"
scalac -color:never -d out type_mismatch.scala 2>&1; echo "exit status: $?"
-- [E007] Type Mismatch Error: type_mismatch.scala:2:19 ------------------------
2 | val count: Int = "three"
| ^^^^^^^
| Found: ("three" : String)
| Required: Int
|
| longer explanation available when compiling with `-explain`
1 error found
exit status: 1
The fix is either the value or the type: val count: Int = 3 when the number is meant, or val count: String = "three" when the word is. The compiler does not guess which one the author meant, and a conversion such as "three".toInt is a call that may fail, which is why it returns a Try or an Option rather than an Int.
A val that is reassigned
val binds a name to a value for good. Assigning to it again is the error below, and the message names the name rather than the expression:
reassign_val.scala
object Reassign:
val total = 1
total = 2
def show: Int = total
scalac -color:never -d out reassign_val.scala 2>&1; echo "exit status: $?"
-- [E052] Type Error: reassign_val.scala:3:8 -----------------------------------
3 | total = 2
| ^^^^^^^^^
| Reassignment to val total
|
| longer explanation available when compiling with `-explain`
1 error found
exit status: 1
The fix is to compute the new value under a new name, or to use a var when the name really changes over time — and then to ask whether the loop that changes it is a fold in disguise.
A match that misses a case
The compiler knows the cases of a sealed type, so it can say which pattern a match would fail on. The message is a warning and the status is 0, which is why a build turns it into an error with -Werror:
missing_case.scala
object MissingCase:
enum Shape:
case Circle(radius: Double)
case Square(side: Double)
def area(shape: Shape): Double = shape match
case Shape.Circle(radius) => math.Pi * radius * radius
scalac -color:never -d out missing_case.scala 2>&1; echo "exit status: $?"
-- [E029] Pattern Match Exhaustivity Warning: missing_case.scala:6:35 ----------
6 | def area(shape: Shape): Double = shape match
| ^^^^^
| match may not be exhaustive.
|
| It would fail on pattern case: Shape.Square(_)
|
| longer explanation available when compiling with `-explain`
1 warning found
exit status: 0
The fix is the missing branch: case Shape.Square(side) => side * side. A case _ would silence the warning and lose the check that makes a new case a compile error, so the warning is worth keeping as an error.
An argument that is missing
A method call is checked against the signature, and a missing argument is reported at the call with the parameter that has no value:
missing_argument.scala
object MissingArgument:
def add(a: Int, b: Int): Int = a + b
def result: Int = add(1)
scalac -color:never -d out missing_argument.scala 2>&1; echo "exit status: $?"
-- [E171] Type Error: missing_argument.scala:3:23 ------------------------------
3 | def result: Int = add(1)
| ^^^^^^
|missing argument for parameter b of method add in object MissingArgument: (a: Int, b: Int): Int
1 error found
exit status: 1
The fix is the second argument, or a default in the definition: def add(a: Int, b: Int = 0): Int. A named argument (add(a = 1, b = 2)) is the form to reach for when the call has several values of the same type.
An if without then
In Scala 3 an if takes then before its branch; the parentheses and braces of Scala 2 are gone, and a missing then is a syntax error before it is a type error:
missing_then.scala
object MissingThen:
val count = 1
if count > 0 println("positive")
scalac -color:never -d out missing_then.scala 2>&1; echo "exit status: $?"
-- [E040] Syntax Error: missing_then.scala:3:34 --------------------------------
3 | if count > 0 println("positive")
| ^
| 'then' expected, but eof found
-- [E008] Not Found Error: missing_then.scala:3:15 -----------------------------
3 | if count > 0 println("positive")
| ^^^^^^^^^^^^^^^^^
| value println is not a member of Boolean
2 errors found
exit status: 1
The fix is one word: if count > 0 then println("positive"). The second error is the consequence of the first, which is a pattern worth knowing: the compiler's first message is the one to fix, and the ones after it may disappear on their own.
Related
- Values and types: the declared type and the inferred one.
- Sealed hierarchies: the exhaustive match the third error is about.
- Methods and parameters: defaults and named arguments.
- WartRemover: the plugin that turns a construct into an error before the compiler's own messages appear.
Article author: Arthur Isaev