E080: Expected Top Level Def
Note: This error was removed before Scala 3.0.0 was released and was never emitted by the Scala 3 compiler.
What it did
This error was triggered in early Dotty versions when the parser expected a top-level definition but found something else.
Example
// Invalid top-level statement
42
Error
-- [E080] Syntax Error: example.scala:1:0 --------------------------------------
1 |42
|^^
|Expected a toplevel definition
Explanation
At the top level of a Scala source file, only definitions are allowed:
packagedeclarationsimportstatementsclass,trait,object,enumdefinitionstypealiasesval,var,defdefinitionsextensiondefinitionsgivendefinitionsexportclauses
Standalone expressions like 42 or println("hello") are not valid at the top level (outside of a definition).
This error was removed in commit 2e5df79218 (May 2021) as part of "Refactor remaining statement and declaration loops". The parser was refactored to provide different error handling for invalid top-level syntax.
To execute code at the top level, wrap it in a main method or object:
@main def run(): Unit =
println("hello")
// Or
object Main:
def main(args: Array[String]): Unit =
println("hello")
In this article