Hello fellow Scala enthusiasts!
Imagine you wrote this code:
def make(): List[Int] =
@tailrec def loop(i: Int, xs: List[Int]): List[Int] =
if i == 0 then xs else loop(i - 1, (dice.nextInt() % n) :: xs)
The error reported by the compiler is as follows:
-- [E007] Type Mismatch Error: /private/tmp/playground/main.scala:24:70 ----------------------------
24 | if i <= 0 then xs else loop(i - 1, (dice.nextInt() % n) :: xs)
| ^
| Found: Unit
| Required: List[Int]
I find this error very confusing and, somewhat embarrassingly, it has caused me to waste a lot of time quite many times. The issue is that the error makes it look like the whole expression has type Unit which is obviously not true. Specifically, because of the location where the problem is reported, I often try to understand why the typer wouldnāt agree with me while the actual error is simply that I forgot to call loop before exiting make.
I find the problem easier to understand with braces:
def make(): List[Int] = {
@tailrec def loop(i: Int, xs: List[Int]): List[Int] = {
if i == 0 then xs else loop(i - 1, (dice.nextInt() % n) :: xs)
}
} // <- error is reported here
Now the error looks like the following:
-- [E007] Type Mismatch Error: /private/tmp/playground/main.scala:26:5 -----------------------------
26 | }
| ^
| Found: Unit
| Required: List[Int]
Because the error is reported after the brace that close make, I find it much easier to understand that the problem is not the type of the expression in loop but the lack of a return value in make.
Is there a way we could improve the first diagnostic?
If thereās a better place to write about this kind of issue, please let me know.

