Back to Scala3

E145: Enumerations Should Not Be Empty

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

3.8.41.4 KB
Original Source

E145: Enumerations Should Not Be Empty

This error occurs when an enum is defined without any cases.

In Scala 3, enums must contain at least one case. An empty enum has no values and cannot be meaningfully used. If you need an empty sealed hierarchy, consider using a sealed trait instead.


Example

scala
enum Empty {}

Error

scala
-- [E145] Syntax Error: example.scala:1:5 --------------------------------------
1 |enum Empty {}
  |     ^^^^^
  |     Enumerations must contain at least one case
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Enumeration Empty must contain at least one case
  | Example Usage:
  |  enum Empty {
  |     case Option1, Option2
  |  }
   -----------------------------------------------------------------------------

Solution

scala
// Add at least one case to the enum
enum Status {
  case Active, Inactive
}
scala
// Or use a sealed trait if no cases are needed yet
sealed trait Empty
<!-- 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>