Enum?
- Posted by Jeremy Cowgar <jeremy at ?o?gar.com> May 14, 2008
- 798 views
I do this all the time and I know others do because I see in it code everywhere. Here are some examples, some of which you will recognize:
constant FILE_NO = 1, -- file number LINE_NO = 2, -- local line number FILE_PTR = 3, -- open file number FILE_START_SYM = 4, -- symbol before start of file OP_WARNING = 5, -- save/restore with/without options OP_TRACE = 6, OP_TYPE_CHECK = 7, OP_PROFILE_TIME = 8, OP_PROFILE_STATEMENT = 9, OP_DEFINES = 10 -- ifdef defines global constant GET_SUCCESS = 0, GET_EOF = -1, GET_FAIL = 1, GET_NOTHING = -2 global constant D_NAME = 1, D_ATTRIBUTES = 2, D_SIZE = 3, D_YEAR = 4, D_MONTH = 5, D_DAY = 6, D_HOUR = 7, D_MINUTE = 8, D_SECOND = 9
Etc... This is fine, but what if you want to add a new element and not at the end? I am sure we've all done it. Well, I borrowed a good thing from other languages called an enum (enumeration)... I've done the code already but have not committed. What do you think?
global enum D_NAME, D_ATTRIBUTES, D_SIZE, ... global enum PERSON_NAME, PERSON_AGE=5, PERSON_DOB, PERSON_EMAIL printf(1, "name=%d, attributes=%d, size=%d\n", { D_NAME, D_ATTRIBUTES, D_SIZE}) printf(1, "person_name=%d, age=%d, dob=%d, email=%d\n", { PERSON_NAME, PERSON_AGE, PERSON_DOB, PERSON_EMAIL}) -- name=1, attributes=2, size=3 -- person_name=1, age=5, dob=6, email=7
An enum is not a new type, it emits a constant. They can be local or global. I think it has a lot of benefit but wanted to get community input. -- Jeremy Cowgar http://jeremy.cowgar.com