考虑以下:
protocol Foo {
typealias A
func hello() -> A
}
protocol FooBar: Foo {
func hi() -> A
}
extension FooBar {
func hello() -> A {
return hi()
}
}
class FooBarClass: FooBar {
typealias A = String
func hi() -> String {
return "hello world"
}
}
此代码编译。但是如果我注释掉关联类型的显式定义typealias A = String
,那么由于某种原因,swiftc 无法推断出类型。
我感觉这与共享相同关联类型但没有通过例如类型参数化(可能关联类型不够强大/不够成熟?)的直接断言的两个协议有关,这使得类型推断模棱两可。
我不确定这是否是语言的错误/不成熟,或者我可能错过了协议扩展中的一些细微差别,这些细微差别理所当然地导致了这种行为。
有人可以对此有所了解吗?