Re: Enum?
- Posted by Jason Gade <jaygade at yah?o.?om> May 15, 2008
- 754 views
Jeremy Cowgar wrote: > > 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: > > }}} <eucode> > 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 > </eucode> {{{ > > 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? > > }}} <eucode> > 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 > </eucode> {{{ > > 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 > <a href="http://jeremy.cowgar.com">http://jeremy.cowgar.com</a> Yes, I think with the ubiquity of sequences and how they are used to simulate structures sometimes, enumerated constants would be a welcome addition. -- A complex system that works is invariably found to have evolved from a simple system that works. --John Gall's 15th law of Systemantics. "Premature optimization is the root of all evil in programming." --C.A.R. Hoare j.