【发布时间】:2017-10-10 19:49:45
【问题描述】:
以下代码在 Swift 3.2 上可以为我工作,但在 Swift 4 的最新版本中,我遇到了一个我无法理解的奇怪错误。
我正在尝试创建一个像这样的通用协议:
public protocol FactoryComponent {
associatedtype Argument
associatedtype Service
static var factory: (Resolver) -> (Argument) -> Service { get }
}
public extension FactoryComponent {
public typealias Factory = (Argument) -> Service
}
在这里使用它:
public extension Container {
@discardableResult
public func register<Component: FactoryComponent>(
factory componentType: Component.Type
) -> ServiceEntry<Component.Factory> { // On this line the error shows
return self.register(componentType.Factory.self) { resolver in
componentType.factory(resolver)
}
}
}
错误:
“组件”没有名为“工厂”的成员类型;您是说“工厂”吗?
当然,自动修复也无济于事,因为错误是无用的......
我检查了 Swift 4 的重大更改,没有看到任何涉及通用协议的内容。
有人能帮我理解这是什么意思吗?
【问题讨论】:
-
一个独立的示例会很有帮助。