-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
flag data type #474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
What is it about bool that prevents the 1-bit optimization? Usually it is implemented as a byte or even a machine word simply for speed. However, I do not know of any reason that it could not conceptually be of type u1. About point 3, I am not sure I understand. Does this mean that a flag is a oneshot (former PLC programmer here)? |
In Zig, Also, in normal structs, if address of bools are not taken, zig is free to make this optimization. |
Interesting. Thanks for the info. |
Er, so this leaves me with the question of what this enhancement is for? The bool type is restricted to "true" and "false" only according to the documentation. I can see that naming the type "flag" makes a more clear indication of the use... It is point 3 in the original proposal that I do not understand. It seems like you get most of the points automatically if you have type name aliasing. I.e. alias bool to flag and you get most of the rest??? I think I am not understanding something about the proposal :-( |
I think this feature proposal has the following problems:
It's likely to be rejected as the discussion stands. Milestones mean that a given issue will be accepted and implemented, rejected, or postponed before the release that the milestone is named after. |
Taking address of the bool.
I do not know this term, but probably yet. Its use case is when you only need to reset the bool value. Perhaps
It is to eliminate mistaken use. This is value that can be only reset, nothing else. It can be viewed as "almost const bool". Similar things: integer ranges in Pascal, distinct types in Nim, or even disabling certain functions (like copy constructor) for classes in C++. |
I'm pretty confident about rejecting this proposal. |
Thinking about it: could such feature be implemented via custom compile time mechanism? Compiler meets unknown word "flag", invokes something, this something replaces it with "bool" and will check how it is used. If it is used incorrectly it will generate compile error. |
When the compiler meets an unknown word, it gives this error:
you are proposing to change this? |
@andrewrk: yes, the compiler could invoke some user supplied "fixing" routine before giving up. Possibly similar to what J. Blow plans to have for Jai (he supports metaprogramming extensively on several levels, and I think he could do this.) |
That sounds like another proposal in a separate issue than a flag data type. |
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?
The text was updated successfully, but these errors were encountered: