gpt4 book ai didi

swift - 为什么我必须执行 append(contentsOf newElements: S) in my custom RangeReplaceableCollection

转载 作者:搜寻专家 更新时间:2023-11-01 06:29:05 26 4
gpt4 key购买 nike

我目前正在尝试将自定义集合类型更新为 Swift 4.1。但是,当我遵守文档并实现 Collection 的所有要求时和 RangeReplaceableCollection , Xcode 还在提示我的类型不符合 RangeReplaceableCollection .

Here's一个mcve对于这个问题(由 Hamish 慷慨提供,谢谢你:)

class Foo<Element : AnyObject> {
required init() {}
private var base: [Element] = []
}

extension Foo : Collection {
typealias Index = Int

var startIndex: Index {
return base.startIndex
}

var endIndex: Index {
return base.endIndex
}

func index(after i: Index) -> Index {
return base.index(after: i)
}

subscript(index: Index) -> Element {
return base[index]
}
}

extension Foo : RangeReplaceableCollection {
func replaceSubrange<C : Collection>(
_ subrange: Range<Index>, with newElements: C
) where Element == C.Element {}
}

According to the documentation ,代码应该编译:

To add RangeReplaceableCollection conformance to your custom collection, add an empty initializer and the replaceSubrange(_:with:) method to your custom type. RangeReplaceableCollection provides default implementations of all its other methods using this initializer and method.

不幸的是,事实并非如此。相反,Xcode 发出以下错误消息:

// error: type 'Foo<Element>' does not conform to protocol 'RangeReplaceableCollection'
// extension Foo : RangeReplaceableCollection {
// ^
// Swift.RangeReplaceableCollection:5:26: note: candidate has non-matching type '<Self, S> (contentsOf: S) -> ()' [with SubSequence = Foo<Element>.SubSequence]
// public mutating func append<S>(contentsOf newElements: S) where S : Sequence, Self.Element == S.Element
// ^
// Swift.RangeReplaceableCollection:9:26: note: protocol requires function 'append(contentsOf:)' with type '<S> (contentsOf: S) -> ()'; do you want to add a stub?
// public mutating func append<S>(contentsOf newElements: S) where S : Sequence, Self.Element == S.Element
//

为了确保这不是文档中的错误,我检查了 the source code of Swift 4.1并找到了 func append<S>(contentsOf newElements: S) where S: Sequence, Element == S.Element 的默认实现在 RangeReplaceableCollection.swift 中,第 442-452 行:

@_inlineable
public mutating func append<S : Sequence>(contentsOf newElements: S) where S.Element == Element {
let approximateCapacity = self.count + numericCast(newElements.underestimatedCount)
self.reserveCapacity(approximateCapacity)
for element in newElements {
append(element)
}
}

问题:

  • 为什么 Xcode 要求实现此功能,尽管提供了默认实现?
  • 如何编译我的代码?

最佳答案

尽管提供了默认实现,但为什么 Xcode 要求实现此功能?

这是由于 Swift 4.1 中引入的 TypeChecker 中的一个错误:
[SR-7429]: Cannot conform a non-final class to a protocol with a defaulted requirement with a generic placeholder constrained to an associated type

如何编译我的代码?

在错误修复之前,目前有三种方法可以编译它。

  • 实现这两个功能:

    • required init<S : Sequence>(_ elements: S) where Element == S.Element {}
    • func append<S : Sequence>(contentsOf newElements: S) where Element == S.Element
  • 将类标记为 final .

  • 将集合类型实现为 struct .为此,required init() {}必须删除。

关于swift - 为什么我必须执行 append<S : Sequence>(contentsOf newElements: S) in my custom RangeReplaceableCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49817152/

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