source: doc/proposals/enums.md @ 46c4dea

Last change on this file since 46c4dea was 46c4dea, checked in by Andrew Beach <ajbeach@…>, 3 weeks ago

Update the enumeration proposal with some updates now that the rework is complete. Deleted the old thesis draft.

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