Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

Programming 1 (BI)
  ▸ exercises
  ▸ quick link

teaching assets


Félévek

* 2025/26/2
* archívum


Linkek

* kalendárium
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C#
* D lang.
* Java
* Nim
* Nim2
  + exercises
* XC=BASIC
* old
  ✦C++, ✦Clojure, ✦Scala


[ edit | logout ]
[ sandbox | passwd ]

enums

In Nim, enums are ordinal types. They have an order, they can be used in case statements, and they can serve as the base type of sets.

Normal enums

Under normal I mean non-pure enums.

type
  Color = enum
    colRed,
    colGreen,
    colBlue

let
  color1: Color = colRed

  color2 = colGreen         # can be used alone

  color3 = Color.colBlue    # can be prefixed with the name of the enum

echo color1     # colRed
echo color2     # colGreen
echo color3     # colBlue

Since these enum values are usually used alone, it's a good idea to give a prefix to these values. Thus, for instance, instead of "green", the value should be called "colGreen". Now, when you see "colGreen" in your code, you'll see that it's an enum value, and it's a color.

Pure enums

type
  Color {.pure.} = enum        # !!! {.pure.} pragma is used !!!
    red, green, blue

let
  color1: Color = Color.red

  #color2 = green              # error: cannot be used alone

  color3 = Color.blue          # MUST be prefixed with the name of the enum

echo color1     # red
echo color3     # blue

Notice the usage of the pure pragma! It tells the compiler that an enum's value cannot be used alone. The enum's value MUST be prefixed with the name of the enum.

From the manual: "An enum type can be marked as pure. Then access of its fields always requires full qualification."

Assigning values to enum members

type
  Direction = enum
    dirUp = "up",
    dirRight = "right",
    dirDown = "down",
    dirLeft = "left"

echo dirUp              # up
echo dirUp.typeOf       # Direction

echo $dirUp             # "up", as string
echo ($dirUp).typeOf    # string

echo ord(dirUp)         # 0
echo ord(dirRight)      # 1

𝥶Here we assign string values to enum members.

You can also assign integer values to enums:

type
  Status = enum
    sOk = 200,
    sNotFound = 404,
    sError = 500

echo sOk            # sOk
echo sOk.typeOf     # Status

echo $sOk           # "sOk" , as string

echo ord(sOk)       # 200

Or even both at once:

type
  Direction = enum
    dirUp = (1, "up"),
    dirRight = "right",    # dirRight = (2, "right"),
    dirDown = "down",      # etc.
    dirLeft = "left"

echo dirUp              # up
echo dirUp.typeOf       # Direction

echo $dirUp             # "up", as string
echo ($dirUp).typeOf    # string

echo ord(dirUp)         # 1
echo ord(dirRight)      # 2
echo ord(dirDown)       # 3
echo ord(dirLeft)       # 4

Get the symbol's name

import std/enumutils      # !!! import this

type
  Direction = enum
    dirUp = (1, "up"),
    dirRight = "right",

echo symbolName(dirUp)    # "dirUp" , as string
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2026 May 03, 22:55