[46c4dea] | 1 | Enumeration Type Proposals |
---|
| 2 | ========================== |
---|
| 3 | With Jiada's recent work on enumerations (see doc/theses/jiada_liang_MMath/), |
---|
| 4 | this is a collection point for some remaining issues with and ideas to |
---|
| 5 | further improve enumerations. |
---|
| 6 | |
---|
| 7 | Fixed Encoding |
---|
| 8 | -------------- |
---|
| 9 | Because Cforall enumerations are encoded using their position, it can be |
---|
[80018f5] | 10 | difficult to give them a stable encoding, this is important in seperate |
---|
| 11 | compilation. |
---|
[46c4dea] | 12 | |
---|
| 13 | The example (provided by Gregor Richards), is a system header that defines |
---|
| 14 | any type that has to be stable across versions. Let's say error codes. |
---|
| 15 | |
---|
| 16 | ```cfa |
---|
| 17 | enum() BigLibError! { |
---|
| 18 | BadArgument, |
---|
| 19 | ... |
---|
| 20 | MissingConfig, |
---|
| 21 | LastStartupError = MissingConfig, |
---|
| 22 | NoMemory, |
---|
| 23 | Timeout, |
---|
| 24 | ... |
---|
| 25 | }; |
---|
| 26 | ``` |
---|
| 27 | |
---|
| 28 | The actual errors are not important, but note that "LastStartupError" has |
---|
| 29 | to be in a particular location relative to some others. If a new version of |
---|
| 30 | the header wants to add a new startup error, it should go before the |
---|
| 31 | LastStartupError, but that will change the position, and hence the encoding, |
---|
| 32 | of all the remaining |
---|
| 33 | |
---|
| 34 | The most obvious example in an existing lanuage I could find is that Rust |
---|
| 35 | usually treats its enum types as opaques algebraic data types, but in certain |
---|
| 36 | cases allows you to fix the encoding of enumerations. |
---|
| 37 | (Although the motivation seems to be optimization of enumerations that |
---|
| 38 | have a lot of common options.) |
---|
| 39 | |
---|
| 40 | Enumerated Arrays |
---|
| 41 | ----------------- |
---|
| 42 | Arrays that use an enumeration as their index. The entire enumeration type |
---|
| 43 | (instead of a subset of int) is used in the index operation. |
---|
| 44 | |
---|
[80018f5] | 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 | |
---|
[46c4dea] | 70 | Although described differently, this is actually a generalization of typed |
---|
| 71 | enumerations, as it can be used to safely represent a constant of any type |
---|
| 72 | for each possible enumeration value. |
---|
| 73 | |
---|
| 74 | ```cfa |
---|
| 75 | extern string colourNames[Colour]; |
---|
| 76 | ``` |
---|
| 77 | |
---|
| 78 | This example is a forward declaration that declares the symbol but does not |
---|
| 79 | give the values or allocate any storage. This is used in header files. |
---|
| 80 | The type of colourNames would be a new type `string[Colour]`. |
---|
| 81 | |
---|
| 82 | In implementation tiles it is safe to give the array's values; |
---|
| 83 | whether it the array has been previously forward declared or not. |
---|
| 84 | ```cfa |
---|
| 85 | string colourNames[Colour] = { |
---|
| 86 | "red", |
---|
| 87 | "violet", |
---|
| 88 | "blue", |
---|
| 89 | // Or without worrying about ordering: |
---|
| 90 | [Green] = "green", |
---|
| 91 | [Orange] = "orange", |
---|
| 92 | [Yellow] = "yellow", |
---|
| 93 | }; |
---|
| 94 | ``` |
---|
| 95 | |
---|
| 96 | The forward declaration and full definition variants allow the user to manage |
---|
| 97 | memory themselves, following the same rules as `extern` variables. |
---|
| 98 | The user can use `const` to fix the values in the array. |
---|
| 99 | These arrays can also be nested `BlendInfo blend[Colour][Colour]` or used |
---|
| 100 | locally. |
---|
| 101 | |
---|
| 102 | Except for the index type (and that the size of the array is fixed per |
---|
| 103 | index type, as it always covers the whole enumeration) it should be the same |
---|
| 104 | as a traditional array. |
---|
| 105 | |
---|
| 106 | Or one of the new safer Cforall arrays, as the features could be combined. |
---|
| 107 | |
---|
[80018f5] | 108 | (Previously, a combined declaration to declare both an enumeration and |
---|
[46c4dea] | 109 | an enumerated array was proposed. That only covers the simple case that |
---|
| 110 | typed enumerations already cover.) |
---|
| 111 | |
---|
| 112 | Enumeration Ranges |
---|
| 113 | ------------------ |
---|
| 114 | We have the simplest iterate over a range of enumerations (can only be used |
---|
| 115 | directly in a for loop, always covers the entire type) but it could be |
---|
| 116 | generalized to work with the other features of ranges, such as going over |
---|
| 117 | just part of the enumeration (see Ranges in doc/proposals/iterators.md). |
---|
| 118 | |
---|
[80018f5] | 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. |
---|
| 129 | |
---|
[46c4dea] | 130 | Flag Set Enumerations |
---|
| 131 | --------------------- |
---|
| 132 | Another common use of enumerations is as a named bitset. |
---|
| 133 | |
---|
| 134 | This doesn't actually follow from the logical definition of enumerations, but |
---|
| 135 | is something that various implementation of "enum" have commonly been used to |
---|
| 136 | recreate. This would formalize that, providing an easy way to create typesafe |
---|
| 137 | implementations of this pattern. |
---|
| 138 | |
---|
| 139 | ```cfa |
---|
| 140 | enum Directions flag { |
---|
| 141 | Up, |
---|
| 142 | Down, |
---|
| 143 | Left, |
---|
| 144 | Right, |
---|
| 145 | Upwards = Up, |
---|
| 146 | Vertical = Up | Down, |
---|
| 147 | }; |
---|
| 148 | ``` |
---|
| 149 | |
---|
[80018f5] | 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 | |
---|
[46c4dea] | 164 | Uses the existing enumeration syntax, except that all initializers must be |
---|
| 165 | bitwise expressions, using only the operators |, & and ~ and, as leaf values, |
---|
| 166 | other labels from the enumeration (no cycles) and 0. |
---|
| 167 | |
---|
| 168 | Each uninitialized label creates a new flag. Every instance of the |
---|
| 169 | enumeration will have each flag be set or unset. The labels act as instances |
---|
| 170 | of the enumeration with only that flag set. |
---|
| 171 | |
---|
| 172 | A type created this way automatically supports: default construction, |
---|
| 173 | from zero_t construction, copy construction, copy assignment, destruction, |
---|
| 174 | equality, inequality and bitwise and (&), or (|) and not (~). |
---|
| 175 | Default construction and from zero_t construction create an instance with no |
---|
| 176 | flags set. Two instances are the same if the same flags are set. |
---|
| 177 | Bitwise operations act on the individual flags in the set. |
---|
| 178 | |
---|
| 179 | In addition the type can be converted to a Boolean. |
---|
| 180 | An flag set is truthy if any flags are set and falsy if no flags are set. |
---|
| 181 | This is not a primitive operation, but comes from combining the zero_t |
---|
| 182 | constructor and inequality. |
---|
| 183 | |
---|
| 184 | Note: Scoping rules are also waiting on the namespacing and module system. |
---|
[80018f5] | 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. |
---|