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
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Nim2
* 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."

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 April 07, 22:10