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