是否可以将枚举(或字符串)转换为其int值?
type
CarBrands = enum
Audi, BMW, Mercedes, Proton
convertsEnumToInt[CarBrands]("Audi") # returns 0
convertsEnumToInt[CarBrands]("Proton") # returns 3
答案 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)