Enum defines sets of named integer constants.
enum-definition: enum id using-declopt enumPropsopt { enumEntry1 ; enumEntry2 ; .. enumEntryn ; } using-decl: using type enum-props: : enum-prop-list enum-prop-list: enum-prop-list, enum-prop enum-prop enum-prop: field access unsafe bitset allow capitalized enumEntry: id id = expr
using specifies which integral type should be used to hold values of the enumeration. Legal types are: int8, byte, int16, word, char, int, and nat.
All enum definitions must contain a zero-valued entry unless the unsafe property is used. The zeroth entry is the default value given to unassigned variables of this type.
If the field access property is present, the enum entries will define fields of the enum type instead of top-level names. This can reduce the risk for name collisions. Because the enum below used the field access property, it introduced a constant named season.summer instead of simply summer. This avoids name conflicts anywhere 'summer' is used in code. To use field access should be the normal case.
Enum types should begin with lower-case letters. Using a leading upper-case letter will render a warning in the compilation buffer unless the allow capitalized property was used.
Examples:
private enum season : field access { winter; spring; summer; fall; }
public enum liOpenGLMode using byte : field access { liOpenGLNone = 0; liOpenGLLowDetail = 1; liOpenGLMediumDetail = 2; liOpenGLHighDetail = 3; }
Enum Type Test
You can use the in operator to check enum collections. When the right-hand side is the name of an enumeration type, the expression is true if the left-hand side matches the integer value of an entry in the enumeration, and false otherwise.
Using the enum type test requires the cm.runtime package.
use cm.runtime; { pln(#season.fall in season); pln(#3 in season); pln(#4 in season); }
Output:
season.fall in season=true 3 in season=true 4 in season=false
Comments
0 comments
Please sign in to leave a comment.