Ignore:
Timestamp:
Dec 16, 2024, 5:59:04 PM (38 hours ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
master
Children:
262a864
Parents:
4167afa
Message:

Attempted to inject some examples (and some other pieces I had missed) into the enum proposal.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/proposals/enums.md

    r4167afa r80018f5  
    88--------------
    99Because Cforall enumerations are encoded using their position, it can be
    10 difficult to give them a stable encoding. The
     10difficult to give them a stable encoding, this is important in seperate
     11compilation.
    1112
    1213The example (provided by Gregor Richards), is a system header that defines
     
    4243(instead of a subset of int) is used in the index operation.
    4344
     45```cfa
     46enum() 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:
     56int jarsOfPaint[Colour] = {0};
     57
     58// Index the array:
     59printf("I have %d jars of blue paint.\n", jarsOfPaint[Blue]);
     60jarsOfPaint[Green] = 3;
     61jarsOfPaint[Red] += 1;
     62
     63// Use the function for higher order programming:
     64int (*lookup)(int collection[Colour], Colour key) = ?[?];
     65
     66// ERROR! Use the enumeration index for safety:
     67jarsOfPaint[0] = 0;
     68```
     69
    4470Although described differently, this is actually a generalization of typed
    4571enumerations, as it can be used to safely represent a constant of any type
     
    80106Or one of the new safer Cforall arrays, as the features could be combined.
    81107
    82 (Previously, a compined declaration to declare both an enumeration and
     108(Previously, a combined declaration to declare both an enumeration and
    83109an enumerated array was proposed. That only covers the simple case that
    84110typed enumerations already cover.)
     
    90116generalized to work with the other features of ranges, such as going over
    91117just part of the enumeration (see Ranges in doc/proposals/iterators.md).
     118
     119This will work best with some alias labels that mark out the beginning of
     120ranges. That is the ranges within the enum will often have to be an
     121intended part of the interface.
     122
     123```cfa
     124for ( kind : DataKind.BeginIntegers +~ DataKind.EndIntegers ) { ... }
     125```
     126
     127Writing the declaration is a bit tricker, because of the lack of aliasing,
     128but this should echo a common C pattern.
    92129
    93130Flag Set Enumerations
     
    111148```
    112149
     150Some example usages:
     151```cfa
     152// If it is exactly Up/Upwards, then set exactly Down
     153if ( 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
    113164Uses the existing enumeration syntax, except that all initializers must be
    114165bitwise expressions, using only the operators |, & and ~ and, as leaf values,
     
    132183
    133184Note: Scoping rules are also waiting on the namespacing and module system.
     185
     186Feature (and Storage) Control
     187-----------------------------
     188Right now features are very coursely grouped. You have exactly three options
     189for your enumeration. However since there are more than two features this
     190means there are some combinations you cannot have.
     191
     192For instance, labels (which are mostly useful for generating debug output)
     193are not available for C style enum, but for both of the new Cforall enums,
     194opaque and typed. However, there is no innate connection between the
     195additional type safety of the opaque enum or the associated values/payloads
     196of the typed enums.
     197
     198Enumerations do interact with on feature that shows this orthagonality,
     199and that is the scoping "no export" marker, that can be applied to any
     200enumeration to change the visibility rules of the enumeration and does not
     201change anything else.
     202
     203This is not urgent, just not using the features you don't want is almost as
     204clear and the compile-time, binary-size and runtime costs are all good enough
     205for now (and some day all of those may have to be improved even when the
     206feature is being used). Isolating independent features is just good design.
Note: See TracChangeset for help on using the changeset viewer.