Closed
Description
This is suggestion for minor feature - boolean like data type with restrictions.
-
flag can be local variable in a function or member of a struct, or item in array, but not function parameter.
-
flag has to be initialized by true or false and the value has to be known at compile time.
var x : flag = false;
-
flag can be assigned only to negation of initial value, also has to be known at compile time.
if (...) x = true;
-
flag can be compared with bool, converted to bool in assignment or passed to bool function parameter.
-
address of flag variable cannot be taken. This protects against unchecked change and also allows the compiler to reduce used memory to 1 bit when member of a struct.
What is it good for?
- Using flag instead of bool makes intent slightly more explicit.
- Forbids misuse of variable intended as flag as free-for-anything.
- Low cognitive overhead - no special syntactic forms are used, just one word gets replaced, with no other changes in the code.
- Flag arrays can be compressed.