From 4f46c09bd43f15f548cd2df9a2b03f094c49140a Mon Sep 17 00:00:00 2001 From: flaviut Date: Mon, 2 Jun 2014 13:35:50 -0400 Subject: [PATCH] Add enums section --- content/enums.md | 55 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/content/enums.md b/content/enums.md index 099c216..16d5ea7 100644 --- a/content/enums.md +++ b/content/enums.md @@ -1,4 +1,57 @@ --- title: Enums --- -# Enums \ No newline at end of file +# 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 +``` + +``` +$ nimrod c -r enums.nim +cdNorth ord: 0 +cdEast ord: 1 +cdSouth ord: 2 +cdWest ord: 3 +3 +FF0000 +00FF00 +0000FF +``` \ No newline at end of file