Back to Scala3

E061: Top Level Can't Be Implicit

docs/_docs/reference/error-codes/E061.md

3.8.41.2 KB
Original Source

E061: Top Level Can't Be Implicit

This error was made inactive in Scala 3.0.0 because top-level implicits are now allowed.

This error was triggered when attempting to define an implicit value or method at the top level of a source file.

Example

scala
package example
implicit val x: Int = 42  // error in old versions

Error

scala
-- [E061] Syntax Error: example.scala:2:0 --------------------------------------
2 |implicit val x: Int = 42
  |^
  |`implicit` modifier cannot be used for top-level definitions

Explanation

In earlier versions of Scala 3, implicit definitions were required to be inside a class, object, or trait. This restriction was removed.

Since Scala 3.0.0 (specifically after PR #5754), top-level implicits are allowed and work correctly:

scala
// This now works in Scala 3
implicit val x: Int = 42

// Or using the modern given syntax
given Int = 42
<!-- SOURCE-ONLY: Remove the notice below once this page has been manually updated. --> <aside class="warning"> This reference page was created with LLM assistance - the description of the error code may not be accurate or cover all possible scenarios. </aside>