Changes in doc/proposals/enums.md [80018f5:46c4dea]
- File:
-
- 1 edited
-
doc/proposals/enums.md (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/proposals/enums.md
r80018f5 r46c4dea 8 8 -------------- 9 9 Because Cforall enumerations are encoded using their position, it can be 10 difficult to give them a stable encoding, this is important in seperate 11 compilation. 10 difficult to give them a stable encoding. The 12 11 13 12 The example (provided by Gregor Richards), is a system header that defines … … 42 41 Arrays that use an enumeration as their index. The entire enumeration type 43 42 (instead of a subset of int) is used in the index operation. 44 45 ```cfa46 enum() Colour {47 Red,48 Violet,49 Blue,50 Green51 Yellow,52 Orange,53 };54 55 // Declare an array with an index of an enumeration:56 int jarsOfPaint[Colour] = {0};57 58 // Index the array:59 printf("I have %d jars of blue paint.\n", jarsOfPaint[Blue]);60 jarsOfPaint[Green] = 3;61 jarsOfPaint[Red] += 1;62 63 // Use the function for higher order programming:64 int (*lookup)(int collection[Colour], Colour key) = ?[?];65 66 // ERROR! Use the enumeration index for safety:67 jarsOfPaint[0] = 0;68 ```69 43 70 44 Although described differently, this is actually a generalization of typed … … 106 80 Or one of the new safer Cforall arrays, as the features could be combined. 107 81 108 (Previously, a com bined declaration to declare both an enumeration and82 (Previously, a compined declaration to declare both an enumeration and 109 83 an enumerated array was proposed. That only covers the simple case that 110 84 typed enumerations already cover.) … … 116 90 generalized to work with the other features of ranges, such as going over 117 91 just part of the enumeration (see Ranges in doc/proposals/iterators.md). 118 119 This will work best with some alias labels that mark out the beginning of120 ranges. That is the ranges within the enum will often have to be an121 intended part of the interface.122 123 ```cfa124 for ( kind : DataKind.BeginIntegers +~ DataKind.EndIntegers ) { ... }125 ```126 127 Writing the declaration is a bit tricker, because of the lack of aliasing,128 but this should echo a common C pattern.129 92 130 93 Flag Set Enumerations … … 146 109 Vertical = Up | Down, 147 110 }; 148 ```149 150 Some example usages:151 ```cfa152 // If it is exactly Up/Upwards, then set exactly Down153 if ( Upwards == dirs ) {154 dirs = Down155 // Otherwise, if a vertical is set, unset them:156 } else if ( Vertical & dirs ) {157 dirs = dirs & ~Vertical;158 // Otherwise, if any direction is set then also set Up:159 } else if ( dirs ) {160 dirs |= Up;161 }162 111 ``` 163 112 … … 183 132 184 133 Note: Scoping rules are also waiting on the namespacing and module system. 185 186 Feature (and Storage) Control187 -----------------------------188 Right now features are very coursely grouped. You have exactly three options189 for your enumeration. However since there are more than two features this190 means there are some combinations you cannot have.191 192 For instance, labels (which are mostly useful for generating debug output)193 are not available for C style enum, but for both of the new Cforall enums,194 opaque and typed. However, there is no innate connection between the195 additional type safety of the opaque enum or the associated values/payloads196 of the typed enums.197 198 Enumerations do interact with on feature that shows this orthagonality,199 and that is the scoping "no export" marker, that can be applied to any200 enumeration to change the visibility rules of the enumeration and does not201 change anything else.202 203 This is not urgent, just not using the features you don't want is almost as204 clear and the compile-time, binary-size and runtime costs are all good enough205 for now (and some day all of those may have to be improved even when the206 feature is being used). Isolating independent features is just good design.
Note:
See TracChangeset
for help on using the changeset viewer.