Back to Scala3

E200: Final Local Def

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

3.8.41.2 KB
Original Source

E200: Final Local Def

This error occurs when the final modifier is used on a local definition (inside a method or block).

The final modifier is only meaningful for class members, where it prevents overriding in subclasses. Local definitions inside methods cannot be overridden, so final has no effect and is not allowed.


Example

scala
def example(): Unit =
  final val local = 42

Error

scala
-- [E200] Syntax Error: example.scala:2:8 --------------------------------------
2 |  final val local = 42
  |        ^^^
  |        The final modifier is not allowed on local definitions

Solution

Remove the final modifier from local definitions:

scala
def example(): Unit =
  val local = 42
  println(local)

If you want an inline constant, use inline val:

scala
def example(): Unit =
  inline val local = 42
  println(local)
<!-- 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>