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-13632] Have Bool conform to CaseIterable #56067

Open
swift-ci opened this issue Sep 30, 2020 · 3 comments
Open

[SR-13632] Have Bool conform to CaseIterable #56067

swift-ci opened this issue Sep 30, 2020 · 3 comments
Labels
improvement standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-13632
Radar None
Original Reporter CTMacUser (JIRA User)
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Improvement
Assignee None
Priority Medium

md5: 96af0f9565de3fb27856bc0b75534452

Issue Description:

The CaseIterable lets users access all states of a type as a Collection. Since Bool only has two states, this should be easy to do. Surprisingly, it hasn't been done yet (as of this writing). Not having this prevents automatic conformance with any type that would otherwise conform except that it has at least one stored Bool property.

@LucianoPAlmeida
Copy link
Collaborator

Do you mean something like

enum E: Bool, CaseIterable { // error
  case a = true
  case b = false
}

_ = E.allCases // [E.a, E.b] 

?

Because in this case, the problem would be that only string, integer, or floating-point literals are supported as raw type on enums.
Which came up to me as a bit surprising. So the solution was to maybe support BooleanLiteral as RawType as well?

We can actually work around this by making bool conforms to `ExpressibleByIntegerLiteral`

extension Bool: ExpressibleByIntegerLiteral {
  public typealias IntegerLiteralType = Int

  public init(integerLiteral value: Int) {
    self = value != 0
  }
}

enum E: Bool, CaseIterable { //Now this compiles and synthetizes the CaseInterable and RawRepresentable
  case a = true
  case b = false
}


_ = E.allCases // [E.a, E.b]

To me, this would be solved by having boolean literal acceptable as raw type.

But, I'm definitely not sure if this is what you meant by having bool conform to `CaseIterable`... if it's not that, can you share an example of a use case?
That would make it easier to understand the intention here 🙂

@typesanitizer
Copy link

I think what Daryle means is having

extension Bool: CaseIterable { ... }

https://forums.swift.org/t/any-particular-reason-that-bool-doesnt-conform-to-caseiterable/40768

@swift-ci
Copy link
Collaborator Author

Comment by Daryle Walker (JIRA)

Yes, what theindigamer (JIRA User) said.

extension Bool: CaseIterable {
    public struct AllCases: RandomAccessCollection {
        @inlinable public var startIndex: Int { return 0 }
        @inlinable public var endIndex: Int { return 2 }

        @inlinable
        public subscript(position: Int) -> Bool {
            switch position {
            case 0: return false
            case 1: return true
            default: preconditionFailure("Out of range subscript position")
            }
        }
        @usableFromInline init() {}
    }

    @inlinable public static var allCases: AllCases { return AllCases() }
}

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

3 participants