Skip to content

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

Closed
PavelVozenilek opened this issue Sep 13, 2017 · 11 comments
Closed

flag data type #474

PavelVozenilek opened this issue Sep 13, 2017 · 11 comments
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@PavelVozenilek
Copy link

PavelVozenilek commented Sep 13, 2017

This is suggestion for minor feature - boolean like data type with restrictions.

  1. flag can be local variable in a function or member of a struct, or item in array, but not function parameter.

  2. flag has to be initialized by true or false and the value has to be known at compile time.

    var x : flag = false;

  3. flag can be assigned only to negation of initial value, also has to be known at compile time.

    if (...) x = true;

  4. flag can be compared with bool, converted to bool in assignment or passed to bool function parameter.

  5. 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?

  1. Using flag instead of bool makes intent slightly more explicit.
  2. Forbids misuse of variable intended as flag as free-for-anything.
  3. Low cognitive overhead - no special syntactic forms are used, just one word gets replaced, with no other changes in the code.
  4. Flag arrays can be compressed.
@andrewrk andrewrk added the enhancement Solving this issue will likely involve adding new logic or components to the codebase. label Sep 13, 2017
@andrewrk andrewrk added this to the 0.2.0 milestone Sep 13, 2017
@kyle-github
Copy link

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)?

@andrewrk
Copy link
Member

In Zig, bool is guaranteed to be 1 bit in a packed struct.

Also, in normal structs, if address of bools are not taken, zig is free to make this optimization.

@kyle-github
Copy link

Interesting. Thanks for the info.

@kyle-github
Copy link

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 :-(

@andrewrk
Copy link
Member

andrewrk commented Sep 14, 2017

I think this feature proposal has the following problems:

  • violates "one obvious way to do things"
  • makes the language bigger and more complicated
  • offers very little ability for programmer to have more explicit intent
  • dubious optimization potential beyond a bool
  • unnecessarily increases the surface area where C and Zig cannot interact cleanly

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.

@PavelVozenilek
Copy link
Author

PavelVozenilek commented Sep 14, 2017

@kyle-github:

What is it about bool that prevents the 1-bit optimization?

Taking address of the bool.

Does this mean that a flag is a oneshot?

I do not know this term, but probably yet. Its use case is when you only need to reset the bool value. Perhaps toggle or something would better name.

what this enhancement is for?

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++.

@andrewrk
Copy link
Member

I'm pretty confident about rejecting this proposal.

@PavelVozenilek
Copy link
Author

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.

@andrewrk
Copy link
Member

When the compiler meets an unknown word, it gives this error:

test.zig:3:12: error: use of undeclared identifier 'flag'
    var a: flag = true;
           ^

you are proposing to change this?

@PavelVozenilek
Copy link
Author

@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.)

@andrewrk
Copy link
Member

That sounds like another proposal in a separate issue than a flag data type.

@tiehuis tiehuis added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Sep 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

4 participants