gpt4 book ai didi

swift 4 : Implementation of a generic protocol with protocol as an associated type

转载 作者:搜寻专家 更新时间:2023-10-31 22:27:25 25 4
gpt4 key购买 nike

我在从 Swift 3.1 到 Swift 4 代码库迁移过程中遇到了一个问题。

当您尝试实现一个通用协议(protocol)方法时,问题就出现了,该方法采用一个带有通用参数的闭包,并将一个协议(protocol)作为关联类型。这比听起来容易:)

以下代码在 Swift 3.1 中运行良好:

protocol FooType {
associatedtype BarType

func foo(bar: BarType)
func foo(action: (BarType) -> Void)
}

protocol Bar {}

class Foo: FooType {
typealias BarType = Bar

// Compiles in both 3.1 and 4
func foo(bar: Bar) {
}

// ERROR: Candidate has non-matching type (Bar) -> Void
func foo(action: (Bar) -> Void) {
}
}

但是在 Swift 4 中编译器给我一个关于类 Foo 不符合协议(protocol) FooTypefoo(action:) 方法实现缺失的错误.

顺便说一句,Xcode 9“修复它”生成了与我相同的实现。

如果我使用 BarType 作为参数类型,代码可以编译,但丢失具体类型信息并不好。

最佳答案

结果是删除行

typealias BarType = Bar

解决了这个问题。这是公平的——类型推断完成了它的工作。

尽管如此,它应该是一个合法的代码,并且看起来像是编译器中的一个错误。

Reported相应地。

关于 swift 4 : Implementation of a generic protocol with protocol as an associated type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44442797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com