Changes between Version 4 and Version 5 of Enum
- Timestamp:
- 08/27/12 10:11:01 (12 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Enum
v4 v5 2 2 3 3 An enumeration is a distinct type consisting of a set of named constants (enumerator list, enumeration elements). [[BR]] 4 By default the underlying type of the enumeration elements is int and the first enumerator 5 has the value 0 with the value of each successive enumerator increased by 1.[[BR]] 6 E numerators can have initialisers to override the default values.4 On platforms that support it and where typed, the underlying type of the enumeration elements is int and the first enumeration element has the value 0 with the value of each successive enumerator increased by 1.[[BR]] 5 6 Elements can have initialisers to override the default values if this is desired. 7 7 8 8 In Cobra an enumeration is declared with the '''enum''' keyword followed by the enumeration name.[[BR]] … … 35 35 Multiple elements can be or'd together by specifying multiple elementNames comma separated 36 36 within the constructor-like call. 37 {{{ 38 #!cobra 39 enum IsNames 40 public, protected, private 41 shared 42 virtual, new, override, abstract 43 ... 44 45 mainMethod.isNames = IsNames(public,shared) 46 }}} 37 47 38 48 == Grammar == … … 40 50 enum <enumName> [ is <accessModifier>] 41 51 [ has <Attributes>] 42 [ of < Type> ]52 [ of <StorageType> ] 43 53 [ <DocString> ] 44 54 <enumItemName> [ = <value> ] … … 49 59 }}} 50 60 51 == Platform ==52 61 53 62 == Examples == … … 55 64 #!cobra 56 65 enum Days 57 """In this enumeration, Sat is 0, Sun is 1, Mon is 2, and so forth."""66 """In this enumeration, (on .Net) Sat is 0, Sun is 1, Mon is 2, and so forth.""" 58 67 Sat 59 68 Sun … … 67 76 def main is shared 68 77 today = Days.Fri 69 yesterday = Day (Thu)78 yesterday = Days(Thu) 70 79 weekend = Days(Sat, Sun) 71 80 72 isWeekend = today in [Days.Sat, Days.Sun] # explicit test enum member inclusion81 isWeekend = today in [Days.Sat, Days.Sun] # explicit test for enum member inclusion 73 82 74 83 … … 79 88 80 89 == Platform == 81 On .Net see the effect of System.!FlagsAttribute on an enum 90 Explicit typing (!StorageType) of enumeration elements and bitwise operations on elements are only supported on platforms that implement enums as variants of numeric Types (.Net) 91 92 For maximum portability and readability try and avoid using initialiser values and use either a direct equality comparison 93 or the set inclusion form for testing enum member inclusion. 94 {{{ 95 #!cobra 96 if today == Day.Fri, ... 97 98 assert CarOptions.SunRoof in options 99 }}} 100 101 102 On .Net note the effect of System.!FlagsAttribute on an enum 82 103 {{{ 83 104 #!cobra … … 106 127 107 128 == Notes == 108 It is suggested all enumerations have an element whose value is 0.[[BR]]129 It is suggested (where typed and initialised) all enumerations have an element whose value is 0.[[BR]] 109 130 If this is in addition to the default starting point of values it should be named '''None'''. 110 131