也許我在這裏失去了一些東西,但與我一起裸露。 說我有一個協議:無法將泛型類型的值轉換爲關聯類型的預期參數
protocol Foo:Hashable, Comparable {}
和具有這個傢伙作爲一個通用的一個結構:
struct UsingFoo<T:Foo> {}
到目前爲止好。說我想第二協議使用Foo
:
protocol Bar {
associatedtype FooType:Foo
func doSomething(with:UsingFoo<FooType>)
}
而上一類使用吧:
class UsingBar<F:Foo>:Bar {
typealias FooType = F
func doSomething(with: UsingFoo<F>) {}
}
現在說我想要把這些傢伙的一方:
class FooBarParty<F:Foo, B:Bar>: NSObject {
var b:B
init(b:B) {
self.b = b
// interestingly, this line below won't compile
// self.b = UsingBar<F>.init()
}
func thisWillCompile() {
UsingBar<F>.init().doSomething(with: UsingFoo<F>.init())
}
func thisWontCompile() {
b.doSomething(with: UsingFoo<F>.init())
}
func thisAlsoWont (anotherB:B) {
anotherB.doSomething(with: UsingFoo<F>.init())
}
}
編譯器說:
Cannot convert value of type 'UsingFoo<F>' to expected argument type 'UsingFoo<_>'
問題是:我如何使用Bar
類型的房產?與往常一樣,任何意見非常讚賞
編輯:感謝接受的答案我想通了,我應該指定FooType
。它應該是這樣的:
class FooBarParty<F:Foo, B:Bar> where B.FooType == F { ... }