Changeset 17df48e
- Timestamp:
- Mar 15, 2017, 2:50:41 PM (8 years ago)
- 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
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/proposals/flags.md
r8ef9c5e7 r17df48e 69 69 In each of the cases above, `FOO` could be replaced by `(BAR | BAZ)` to do the same operation or test on multiple flags at once. 70 70 71 ### Alternative/Additional Features ### 72 73 #### User-defined enum discriminant iterator #### 74 It 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 #### 86 As 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 71 96 ### Related Work ### 72 97 C# 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.