<commit_before>---
layout: doc-page
title: "Translation of Enums and ADTs"
---

The compiler expands enums and their cases to code that only uses
Scala's other language features. As such, enums in Scala are
convenient _syntactic sugar_, but they are not essential to understand
Scala's core.

We now explain the expansion of enums in detail. First,
some terminology and notational conventions:

 - We use `E` as a name of an enum, and `C` as a name of a case that appears in `E`.
 - We use `<...>` for syntactic constructs that in some circumstances might be empty. For instance,
   `<value-params>` represents one or more parameter lists `(...)` or nothing at all.

 - Enum cases fall into three categories:

   - _Class cases_ are those cases that are parameterized, either with a type parameter section `[...]` or with one or more (possibly empty) parameter sections `(...)`.
   - _Simple cases_ are cases of a non-generic enum that have neither parameters nor an extends clause or body. That is, they consist of a name only.
   - _Value cases_ are all cases that do not have a parameter section but that do have a (possibly generated) `extends` clause and/or a body.

  Simple cases and value cases are collectively called _singleton cases_.

The desugaring rules imply that class cases are mapped to case classes, and singleton cases are mapped to `val` definitions.

There are nine desugaring rules. Rule (1) desugar enum definitions. Rules
(2) and (3) desugar simple cases. Rules (4) to (6) define `extends` clauses for cases that
are missing them. Rules (7) to (9) define how such cases with `extends` clauses
map into `case class`es or `val`s.

1. An `enum` definition
   ```scala
   enum E ... { <defs> <cases> }
   ```
   expands to a `sealed abstract` class that extends the `scala.Enum` trait and
   an associated companion object that contains the defined cases, expanded according
   to rules (2 - 8). The enum class starts with a compiler-generated import that imports
   the names `<caseIds>` of all cases so that they can be used without prefix in the class.
   ```scala
   sealed abstract class E ... extends <parents> with scala.Enum {
     import E.{ <caseIds> }
      <defs>
   }
   object E { <cases> }
   ```

2. A simple case consisting of a comma-separated list of enum names
   ```scala
   case C_1, ..., C_n
   ```
   expands to
   ```scala
   case C_1; ...; case C_n
   ```
   Any modifiers or annotations on the original case extend to all expanded
   cases.

3. A simple case
   ```scala
   case C
   ```
   of an enum `E` that does not take type parameters expands to
   ```scala<commit_msg>update typo in docs to scala.reflect.Enum<commit_after>---
layout: doc-page
title: "Translation of Enums and ADTs"
---

The compiler expands enums and their cases to code that only uses
Scala's other language features. As such, enums in Scala are
convenient _syntactic sugar_, but they are not essential to understand
Scala's core.

We now explain the expansion of enums in detail. First,
some terminology and notational conventions:

 - We use `E` as a name of an enum, and `C` as a name of a case that appears in `E`.
 - We use `<...>` for syntactic constructs that in some circumstances might be empty. For instance,
   `<value-params>` represents one or more parameter lists `(...)` or nothing at all.

 - Enum cases fall into three categories:

   - _Class cases_ are those cases that are parameterized, either with a type parameter section `[...]` or with one or more (possibly empty) parameter sections `(...)`.
   - _Simple cases_ are cases of a non-generic enum that have neither parameters nor an extends clause or body. That is, they consist of a name only.
   - _Value cases_ are all cases that do not have a parameter section but that do have a (possibly generated) `extends` clause and/or a body.

  Simple cases and value cases are collectively called _singleton cases_.

The desugaring rules imply that class cases are mapped to case classes, and singleton cases are mapped to `val` definitions.

There are nine desugaring rules. Rule (1) desugar enum definitions. Rules
(2) and (3) desugar simple cases. Rules (4) to (6) define `extends` clauses for cases that
are missing them. Rules (7) to (9) define how such cases with `extends` clauses
map into `case class`es or `val`s.

1. An `enum` definition
   ```scala
   enum E ... { <defs> <cases> }
   ```
   expands to a `sealed abstract` class that extends the `scala.reflect.Enum` trait and
   an associated companion object that contains the defined cases, expanded according
   to rules (2 - 8). The enum class starts with a compiler-generated import that imports
   the names `<caseIds>` of all cases so that they can be used without prefix in the class.
   ```scala
   sealed abstract class E ... extends <parents> with scala.reflect.Enum {
     import E.{ <caseIds> }
      <defs>
   }
   object E { <cases> }
   ```

2. A simple case consisting of a comma-separated list of enum names
   ```scala
   case C_1, ..., C_n
   ```
   expands to
   ```scala
   case C_1; ...; case C_n
   ```
   Any modifiers or annotations on the original case extend to all expanded
   cases.

3. A simple case
   ```scala
   case C
   ```
   of an enum `E` that does not take type parameters expands to
   ```scala
{'fraction_non_alphanumeric': 0.08241965973534972, 'fraction_numerical': 0.005293005671077505, 'mean_word_length': 3.3156606851549757, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1547965', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1449, 'n_words': 794}