Nominal type as a generic constraint

That was me alluding to a possible future direction. A hypothetical Bool could look like:

// In the standard library
protocol StaticCaseIterable {
  associatedtype AllCases : Collection = [Self] where Self == Self.AllCases.Element
  const static let allCases: AllCases { get }
}

// In my library

import WrappedLibrary
struct MyBool: ExpressibleByBooleanLiteral, Equatable, StaticCaseIterable {
  let _base: WrappedLibrary.Bool

  @_transparent
  init(booleanLiteral value: Bool) {
    _base = value ? WrappedLibrary.bool_true : WrappedLibrary.bool_false
  }

  @_transparent
  static func == (a: Self, b: Self) { ... }

  const static var allCases: [Self] { [true, false] }
}

Are there cases where you think exhaustive checking is not the answer? Also, do you think it would be safe if your API mandated enums but users could easily just add a default case when switching over an enum, or are you looking for a compiler feature to warn against default cases even on enums?

1 Like