Skip to content

Instantly share code, notes, and snippets.

@r-peck
Created June 10, 2019 20:32
Show Gist options
  • Save r-peck/bc9fff0d54e3beea77d3beb0654bed0c to your computer and use it in GitHub Desktop.
Save r-peck/bc9fff0d54e3beea77d3beb0654bed0c to your computer and use it in GitHub Desktop.
Inferring nested associated types with opaque return types
protocol A { }
struct AImpl: A {
init() { }
}
protocol B {
associatedtype BA: A
var a: BA { get }
}
struct BImpl: B {
init() { }
var a: some A {
return AImpl()
}
}
protocol C: B {
associatedtype CB: B where BA == CB.BA
var b: CB { get }
}
extension C {
var a: BA {
return b.a
}
}
struct CImpl: C { // Type 'CImpl' does not conform to protocol 'B', wants to insert typealias for BA
var b: some B {
return BImpl()
}
}
//struct CImpl: C { // 'C' requires the types 'CImpl.BA' (aka 'some A') and '(some B).BA' to be equivalent
// typealias BA = BImpl.BA
//
// var b: some B {
// return BImpl()
// }
//}
//struct CImpl: C { // Works but requires being explicit with types
// typealias BA = BImpl.BA
//
// var b: BImpl {
// return BImpl()
// }
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment