|
Oktatás * Programozás 1 * Szkriptnyelvek Teaching • Programming 1 (BI) Félévek Linkek * kalendárium |
Nim2 /
enumsIn Nim, enums are ordinal types. They have an order, they can be used in Normal enumsUnder 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 " Pure enumstype 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 From the manual: "An enum type can be marked as pure. Then access of its fields always requires full qualification." Assigning values to enum memberstype 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 nameimport std/enumutils # !!! import this type Direction = enum dirUp = (1, "up"), dirRight = "right", echo symbolName(dirUp) # "dirUp" , as string |
![]() Blogjaim, hobbi projektjeim * The Ubuntu Incident [ edit ] |