Changeset 17df48e


Ignore:
Timestamp:
Mar 15, 2017, 2:50:41 PM (8 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
5a3ac84
Parents:
8ef9c5e7
Message:

Update flags proposals with some newer ideas

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/proposals/flags.md

    r8ef9c5e7 r17df48e  
    6969In each of the cases above, `FOO` could be replaced by `(BAR | BAZ)` to do the same operation or test on multiple flags at once.
    7070
     71### Alternative/Additional Features ###
     72
     73#### User-defined enum discriminant iterator ####
     74It may be useful to provide a more general method for changing the enum discriminant assignment function, e.g. the flag enum discriminants could be defined by something like the following:
     75
     76        ```
     77        enum(@ << 1) TCP_Flags {  // each discriminant is left-shifted by 1 from the previous
     78                FIN = 0x1,  // first flag is 1
     79                SYN,
     80                ACK,
     81                ...
     82        }
     83        ```
     84
     85#### Member expression for enums ####
     86As a more ergonomic way to set and unset enum flags, we could define a member expression for flags enums. Since only unions and structs can have member expressions now, this change would be backwards compatible. Basically, given a `FunFlags f`, `f.FOO` would return a proxy object which could be implicitly converted to `bool` (with semantics `f & FOO`, i.e. "check if `FOO` is set on `f`"), as well as having `bool` assigned to it (with semantics `f |= FOO` on true or `f -= FOO` on false, i.e. "set or unset `FOO` on `f` as appropriate"). With this member function, the operations above can be expressed as follows (possibly more ergonomically):
     87
     88        ```
     89        FunFlags f = some_val();
     90        if ( f.FOO ) { sout | "f has FOO set" | endl; }
     91        f.FOO = true;    // set FOO
     92        f.FOO = false;   // unset FOO
     93        f.FOO = ! f.FOO; // toggle FOO
     94        ```
     95
    7196### Related Work ###
    7297C# has the [`[Flags]`][1] enum attribute, but their proposal does not go as far; specifically, the flag discriminants must be manually specified, and they do not automatically implement the bitwise operators on the flags.
Note: See TracChangeset for help on using the changeset viewer.