Skip to content
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

[SR-13171] Documentation for OptionSet should reference .union() rather than Array of OptionSet instances #55613

Closed
swift-ci opened this issue Jul 7, 2020 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. good first issue Good for newcomers standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Jul 7, 2020

Previous ID SR-13171
Radar None
Original Reporter dfsweeney (JIRA User)
Type Bug
Status Closed
Resolution Invalid
Environment

Xcode 12.0 beta2

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug, StarterBug
Assignee dfsweeney (JIRA)
Priority Medium

md5: a910a342426be1d9ec5720cf9181573a

Issue Description:

The documentation for OptionSet reads:

Declare additional preconfigured option set values as static properties initialized with an array literal containing other option values.

The code example shows static properties with arrays of the option values.

The documentation should read something like:

Declare combinations of option values as static properties using the .union() function to combine other option values into larger sets.

and the code example should be:

struct ShippingOptions: OptionSet {
    let rawValue: Int


    static let nextDay    = ShippingOptions(rawValue: 1 << 0)
    static let secondDay  = ShippingOptions(rawValue: 1 << 1)
    static let priority   = ShippingOptions(rawValue: 1 << 2)
    static let standard   = ShippingOptions(rawValue: 1 << 3)


    static let express: ShippingOptions = ShippingOptions.nextDay
                                            .union( ShippingOptions.secondDay)
    static let all: ShippingOptions = ShippingOptions.express
                                            .union(ShippingOptions.priority)
                                            .union(ShippingOptions.standard)
}

Using an array defeats the bitmap because each of the options takes up its own slot in the array. The 'express' ShippingOptions above uses two Ints in storage instead of one, and the all Shipping options uses three Ints.

using .union() sets the bits in the bitmap so all of the options use constant storage (one Int).

It's possible that I am misunderstanding what the documentation says or intends here. If so please let me know. If I do understand this right, I can create a pull request with the new text and send it through. Thanks!

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jul 8, 2020

Comment by Daniel Sweeney (JIRA)

Oh, I see now. This one's not a bug. There's logic to turn the array of OptionSet entries into a rawValue with their bitwise or. I'm not sure why I did not see it working the first time I tested it. I'll close the bug.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jul 8, 2020

Comment by Daniel Sweeney (JIRA)

I misunderstood how the array argument works. This was not a bug and the documentation is OK.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. good first issue Good for newcomers standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

1 participant