Last active
May 11, 2020 05:42
-
-
Save dabrahams/a19f3de92872ed2f296744b510917a69 to your computer and use it in GitHub Desktop.
Doug Gregor's demonstration of conformance ambiguity from https://forums.swift.org/t/ergonomics-generic-types-conforming-in-more-than-one-way/34589/59
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// (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