Changeset 80018f5 for doc/proposals
- Timestamp:
- Dec 16, 2024, 5:59:04 PM (41 hours ago)
- Branches:
- master
- Children:
- 262a864
- Parents:
- 4167afa
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/proposals/enums.md
r4167afa r80018f5 8 8 -------------- 9 9 Because Cforall enumerations are encoded using their position, it can be 10 difficult to give them a stable encoding. The 10 difficult to give them a stable encoding, this is important in seperate 11 compilation. 11 12 12 13 The example (provided by Gregor Richards), is a system header that defines … … 42 43 (instead of a subset of int) is used in the index operation. 43 44 45 ```cfa 46 enum() Colour { 47 Red, 48 Violet, 49 Blue, 50 Green 51 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 44 70 Although described differently, this is actually a generalization of typed 45 71 enumerations, as it can be used to safely represent a constant of any type … … 80 106 Or one of the new safer Cforall arrays, as the features could be combined. 81 107 82 (Previously, a com pined declaration to declare both an enumeration and108 (Previously, a combined declaration to declare both an enumeration and 83 109 an enumerated array was proposed. That only covers the simple case that 84 110 typed enumerations already cover.) … … 90 116 generalized to work with the other features of ranges, such as going over 91 117 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 of 120 ranges. That is the ranges within the enum will often have to be an 121 intended part of the interface. 122 123 ```cfa 124 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. 92 129 93 130 Flag Set Enumerations … … 111 148 ``` 112 149 150 Some example usages: 151 ```cfa 152 // If it is exactly Up/Upwards, then set exactly Down 153 if ( Upwards == dirs ) { 154 dirs = Down 155 // 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 ``` 163 113 164 Uses the existing enumeration syntax, except that all initializers must be 114 165 bitwise expressions, using only the operators |, & and ~ and, as leaf values, … … 132 183 133 184 Note: Scoping rules are also waiting on the namespacing and module system. 185 186 Feature (and Storage) Control 187 ----------------------------- 188 Right now features are very coursely grouped. You have exactly three options 189 for your enumeration. However since there are more than two features this 190 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 the 195 additional type safety of the opaque enum or the associated values/payloads 196 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 any 200 enumeration to change the visibility rules of the enumeration and does not 201 change anything else. 202 203 This is not urgent, just not using the features you don't want is almost as 204 clear and the compile-time, binary-size and runtime costs are all good enough 205 for now (and some day all of those may have to be improved even when the 206 feature is being used). Isolating independent features is just good design.
Note: See TracChangeset
for help on using the changeset viewer.