Skip to content

Instantly share code, notes, and snippets.

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 JensAyton/e5dcc905cc09dca62529dbd7393ecf5d to your computer and use it in GitHub Desktop.
Save JensAyton/e5dcc905cc09dca62529dbd7393ecf5d to your computer and use it in GitHub Desktop.
Reduced case where Swift produces a warning with a fix-it that removes its ability to infer types in otherwise working code.
// Yes, this is reduced from code that works and does something arguably useful.
protocol P1 {
associatedtype A1
func f(_: A1)
}
protocol P2: P1 {
associatedtype A2: P2
var a2: A2 { get }
// Warning: Typealias overriding associated type 'A1' from protocol 'P1' is better
// expressed as same-type constraint on the protocol [with fix-it]
typealias A1 = A2.A1
}
extension P2 {
func f(_: A1) {}
}
struct T1: P2 {
struct A1 {}
var a2: Self { self }
}
struct T2: P2 {
var a2: some P2 { T1() }
// If I follow the warning’s advice, *or* remove f() from P1, this is required:
// typealias A1 = A2.A1
}
// Things that don’t work:
// • Flattening P2 into P1. It still requires the extra typealias in T2.
// • Using the latest toolchain build with all the -requirment-machine-... flags set to on.
// • Making a marker protocol for terminal cases like T1 and constraining P2 where A2: TerminalP2:
//
// protocol TerminalP2: P2 {
// var a2: Self { get }
// }
//
// extension TerminalP2 {
// var a2: Self { self }
// }
//
// extension P2 where A2: TerminalP2 {
// typealias A1 = A2.A1
// }
//
// This actually causes a new hilarious error,
// Type 'Self.A2' does not conform to protocol 'TerminalP2' on line 18.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment