Skip to content

Improve ergonomics - #25

Draft
jmert wants to merge 4 commits into
masterfrom
ergonomics
Draft

Improve ergonomics#25
jmert wants to merge 4 commits into
masterfrom
ergonomics

Conversation

@jmert

@jmert jmert commented Sep 2, 2026

Copy link
Copy Markdown
Owner

Improve ergonomics of the bit flag types by expanding coverage / utility of
bitwise operations and type-generic helpers.

  1. Define zero and iszero to help with type-generic coding patterns.

    julia> @bitflag Flag::UInt8 v1 v2 v4
    
    julia> iszero(v1 & v4)
    true
    
    julia> zeros(Flag, 2)
    2-element Vector{Flag}:
     reinterpret(Flag, 0x00)::Flag = 0x00
     reinterpret(Flag, 0x00)::Flag = 0x00
  2. Define in to test the inclusion of a bit pattern within another, with special-case
    handling for zero-valued flags in definitions that include an explicit zero — a zero
    on the left-hand side is "in" the pattern on the right if and only if the right is
    also zero (since otherwise all bit patterns include the zero value).

    julia> v1 in (v1 | v2)
    true
    
    julia> zero(Flag)  zero(Flag)  # no 0-valued flag in Flag
    false
    
    julia> @bitflag HasEmpty::UInt8 empty=0 value1 value2
    
    julia> zero(HasEmpty) in (value1 | value2)
    false
    
    julia> zero(HasEmpty) in empty  # only because zero(HasEmpty) == empty
    true
  3. Add bitwise operations for NOT (~) and XOR (), and change all bitwise operators
    to produce "invalid" bit patterns in order to: (a) better support the iszero(x & y)
    condition for flag types which do not include an explicit zero-valued flag, and
    (b) allow for a practical definition of ~.

    julia> v1 & v2
    reinterpret(Flag, 0x00)::Flag = 0x00  # no 0-valued flag
    
    julia> ~(v1 | v2 | v4)
    reinterpret(Flag, 0xf8))::Flag = 0xf8  # complement of all flags

Fixes #24

@codecov

codecov Bot commented Sep 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (36f42a6) to head (dbc85f5).

Additional details and impacted files
@@            Coverage Diff            @@
##            master       #25   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines          202       220   +18     
=========================================
+ Hits           202       220   +18     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Note that membership of the zero-valued flag is a special case;
we choose to allow 0 ∈ 0 (but 0 ∉ N for N ≠ 0) only if there is a valid
flag whose value is zero.
…ions

Rather than passing through the type constructor, simply allow the
bitwise operations to construct arbitrary bit patterns. This makes the
test for the empty flag easier (through bitwise AND) to perform for
flags which do not include an explicit 0-value:

```julia
julia> @bitflag Flag::UInt8 v1 v2 v4

julia> iszero(v1 & v4)
true
```
The non-error condition is also essentially required to allow a
meaningful bitwise NOT to be defined:
```julia
julia> ~(v1 | v2 | v4)
reinterpret(Flag, 0xf8))::Flag = 0xf8
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

api suggestions

1 participant