Skip to content

Instantly share code, notes, and snippets.

@dabrahams
Last active May 11, 2020 05:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dabrahams/a19f3de92872ed2f296744b510917a69 to your computer and use it in GitHub Desktop.
Save dabrahams/a19f3de92872ed2f296744b510917a69 to your computer and use it in GitHub Desktop.
// (A) Definition
protocol P {
static var isEquatable: Bool { get }
}
// (B) Default implementation
extension P {
static var isEquatable: Bool { false }
}
// (C) More-constrained default implementation
extension P where Self : Equatable {
static var isEquatable: Bool { true }
}
// Phases of Doug's explanation, in order.
// Replace one false with true to experience earlier phases.
#if false
// (D) Conformance
extension Array : P {}
#elseif false
// (D') Alternative, conditional conformance
extension Array : P where Element: Equatable {}
#elseif false
extension Array : P where Element: Equatable {}
struct X: Equatable, Q {}
let a = Array<X>() // Array<X>'s conformance to P should be ambiguous
#else
// (D'') conformance with an ambiguity
extension Array : P where Element: Equatable, Element: Q {}
#endif
protocol Q { }
// (E) Another constrained implementation
extension P where Self : Q {
static var isEquatable: Bool { true }
}
// (F) Array conditionally conforms to Q
extension Array: Q where Element: Q { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment