<commit_before>---
title: Enums
---
# Enums
Enums in Nimrod are like enums in C, but are type-checked. There are no anonymous enums in Nimrod.

``` nimrod
type
  # Note the Hungarian notation, used to avoid conflicts
  CompassDirections = enum
    cdNorth, cdEast, cdSouth, cdWest
  # {.pure.} means it values must be prefixed with the enum's name
  Colors {.pure.} = enum
    Red = "FF0000", Green = (1, "00FF00"), Blue = "0000FF"
  # Enums can have holes in them
  # Not recommended, exists only for interoperability with C
  Signals = enum
    sigQuit = 3, sigAbort = 6, sigKill = 9

# Iterates through the values
for direction in cdNorth..cdWest:
  # `ord()` returns the integer value of the enum
  # `CompassDirections()` is the inverse of `ord()`
  echo CompassDirections(ord(direction)), " ord: ", ord(direction)

# `ord` gives the integer value, even if there are holes
echo ord(sigQuit)

# Enums are ordinal and have `inc` and `dec`
var ordinal = low(int)
inc ordinal
dec ordinal
echo high(char)

when false:
  var nonOrdinal = sigQuit
  # `Signals` has holes and so isn't ordinal,
  # so `inc` and `dec` don't work
  inc nonOrdinal
  dec nonOrdinal

# The custom `$` makes it echo the hex value
for color in Colors.Red..Colors.Blue:
  echo color
```

```console
$ nimrod c -r enums.nim
cdNorth ord: 0
cdEast ord: 1
cdSouth ord: 2
cdWest ord: 3
3
�
FF0000
00FF00
0000FF

```<commit_msg>Make enum section clearer<commit_after>---
title: Enums
---
# Enums
Enums in Nimrod are like enums in C, but are type-checked. There are no anonymous enums in Nimrod.

``` nimrod
type
  CompassDirections = enum
    cdNorth, cdEast, cdSouth, cdWest

  Colors {.pure.} = enum
    Red = "FF0000", Green = (1, "00FF00"), Blue = "0000FF"

  Signals = enum
    sigQuit = 3, sigAbort = 6, sigKill = 9
```
Notice that each element in `CompassDirections` is prepended with `cd` to avoid name conflicts since references to the enum value do not need to be qualified. The `{.pure.}` pragma that `Colors` has requires that all references to `Colors`'s values be qualified, therefore making a prefix unnecessary.

Enums can be given custom values and stringify values, as shown by `Colors` and `Signals`.

While enums can also have disjoint values, it should not be used for any other reason than compatibility with C because it breaks the idea that enums are ordinal.

``` nimrod
for direction in ord(low(CompassDirections))..
                 ord(high(CompassDirections)):
  echo CompassDirections(direction), " ord: ", direction

var ordinal = low(int)
inc ordinal
dec ordinal
echo high(char)
```
```console
$ nimrod c -r enums.nim
cdNorth ord: 0
cdEast ord: 1
cdSouth ord: 2
cdWest ord: 3
3
�
```

Because enums are ordinals, they have the `low`, `high`, `inc`, `dec`, and `ord` methods defined, where

 - `low` gives the lowest possible value
 - `high` give the highest possible value
 - `inc` increments
 - `dec` decrements
 - `ord` gives the integer value of the enum
 - `CompassDirections` gives the enum value from an integer

It is also possible to iterate through all possible values of ordinal enums, either as shown above, or `cdNorth..cdWest`, which is equivalent.


``` nimrod
when false:
  var nonOrdinal = sigQuit
  inc nonOrdinal
  dec nonOrdinal
```

`Signals` is not an ordinal type, and so doesn't have the `inc` and `dec` procedures.
{'fraction_non_alphanumeric': 0.0958945160323644, 'fraction_numerical': 0.016182199580461493, 'mean_word_length': 3.809798270893372, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '715952', 'n_tokens_mistral': 1145, 'n_tokens_neox': 1035, 'n_words': 468}