Back to Scala3

E156: Modifier Not Allowed For Definition

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

3.8.41.3 KB
Original Source

E156: Modifier Not Allowed For Definition

This error occurs when a modifier is used on a definition where it is not applicable. Different kinds of definitions (classes, objects, methods, etc.) support different sets of modifiers.

Common invalid combinations include:

  • opaque on a method definition (only valid for type aliases)
  • abstract on an object definition
  • sealed on an object definition

Example

scala
object Test {
  opaque def example: Int = 42
}

Error

scala
-- [E156] Syntax Error: example.scala:2:13 -------------------------------------
2 |  opaque def example: Int = 42
  |  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |  Modifier opaque is not allowed for this definition

Solution

scala
object Test {
  // opaque is only valid for type aliases
  opaque type PositiveInt = Int

  // Regular method definition
  def example: Int = 42
}
scala
// abstract is not allowed on object - use trait or abstract class instead
abstract class Base {
  def example: Int
}
<!-- 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>