将Nim枚举转换为整数

时间:2019-06-30 04:58:14

标签: enums enumeration nim

是否可以将枚举(或字符串)转换为其int值?

type
    CarBrands = enum
        Audi, BMW, Mercedes, Proton

convertsEnumToInt[CarBrands]("Audi") # returns 0
convertsEnumToInt[CarBrands]("Proton") # returns 3

1 个答案:

答案 0 :(得分:1)

根据documentation,您可以使用ord函数。

type
  CompassDirections = enum
    cdNorth, cdEast, cdSouth, cdWest

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)