gpt4 book ai didi

arrays - 如何将扩展仅应用于泛型的某些特化?

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

当扩展仅对泛型的某些特殊化有效时,是否有一种方法可以将扩展应用于泛型以使其符合协议(protocol)?

例如,考虑这个协议(protocol),它返回一个字典,计算符合该协议(protocol)的实例所包含的值的频率:

// a type conforming to this protocol should return a dictionary
// which counts the frequencies of values contained by the type instance
protocol ConvertibleToFrequencyDictionary {
typealias ItemType
func dictionaryCountingFrequencies<ItemType:Hashable>() -> Dictionary<ItemType,Int>
}

因为被计数的值必须作为字典的键,所以这些值必须是符合 Hashable 的类型。这由泛型 dictionaryCountingFrequencies 定义的类型约束表示。方法。 (我没有看到任何直接在关联类型上定义类型约束的方法,例如,在“typealias”声明中。)

现在考虑 Array 上的这个扩展,旨在使其符合协议(protocol):

extension Array : ConvertibleToFrequencyDictionary {
typealias ItemType=Element
func dictionaryCountingFrequencies<ItemType:Hashable>() -> Dictionary<ItemType,Int> {
var valueToCount = Dictionary<ItemType,Int>()
for item in self {
if let existingCount = valueToCount[item] {
valueToCount.updateValue(value: existingCount + 1, forKey: item)
} else {
valueToCount.updateValue(value: 1, forKey: item)
}
}
return valueToCount;
}
}

这应该返回数组中出现的每个不同值的频率。但是当然,由于这些值必须是可散列的,所以这个扩展应该只在应用于 Array<T:Hashable> 时才有效。 .

但这不适用于 Array<Int> ,即使 Int 是 Hashable。

为什么不呢?如果您在泛型上编写扩展,那么该扩展是否必须能够适用于泛型的所有可能的特化?

最佳答案

您可以扩展 SequenceType 而不是 Array,这为您提供了更多类型,这也适用,然后您可以限制 Element 的类型在哪里

  • Array 扩展需要 where Element : SomeProtocol
  • SequenceType 扩展需要 where Generator.Element : SomeProtocol

您遇到的问题不是 Int 不是 Hashable,而是 Item 不一定是 Hashable。转换为 ItemType 也有效,因为在此示例中它始终是 Hashable

我个人不会为此使用泛型。由于 Element 已经涵盖了您可以存储在数组中的所有内容,因此无需使其类型模糊。

extension SequenceType where Generator.Element : Hashable {

func dictionaryCountingFrequencies() -> Dictionary<Generator.Element,Int> {
var valueToCount = Dictionary<Generator.Element,Int>()
for item in self {

if let existingCount = valueToCount[item] {
valueToCount.updateValue(existingCount + 1, forKey: item)
} else {
valueToCount.updateValue(1, forKey: item)
}
}
return valueToCount;
}
}

let array = [1,2,3,4,5,1,1,1,2,3,3]

let freqDict = array.dictionaryCountingFrequencies()
// prints [5: 1, 2: 2, 3: 3, 1: 4, 4: 1]
freqDict[5]
// prints 1

关于arrays - 如何将扩展仅应用于泛型的某些特化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24396362/

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