为什么这段代码含糊不清?

时间:2016-08-07 20:41:08

标签: swift generics swift2 ambiguous

我已将此扩展程序写入SequenceType以模仿Python's collections.Counter

let input = [
    "a", "a", "a", "a", "a",
    "b", "b", "b", "b",
    "c", "c", "c",
    "d", "d",
    "e"
]


let counts = input.countRepetitions()

print(counts) //expected result: ["a": 5 , "b" : 4, "c" : 3, "d" : 2, "e" : 1]

以下是代码:

extension SequenceType where Self.Generator.Element : Hashable {
    func countRepetitions() -> [Self.Generator.Element : Int] {
        return self.reduce([Self.Generator.Element : Int]()) { dict, element in
            dict[key: element] = (dict[element] ?? 0) + 1
        }
    }
}

我收到以下错误:

Playground execution failed: OS X.playground:26:22: error: type of expression is ambiguous without more context
                return self.reduce([Self.Generator.Element : Int]()) { dict, element in
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1 个答案:

答案 0 :(得分:6)

在使用嵌套定义变量时,这似乎是一个限制 类型。而

let foo1: [Self.Generator.Element : Int] = [:]

在你的方法中编译,这不是:

let foo2 = [Self.Generator.Element : Int]()
//  error: type of expression is ambiguous without more context

作为解决方法,您可以定义一个typealias:

typealias E = Self.Generator.Element
let foo3 = [E : Int]()

适用于您的案件:

extension SequenceType where Self.Generator.Element : Hashable {
    func countRepetitions() -> [Self.Generator.Element : Int] {
        typealias E = Self.Generator.Element
        return self.reduce([E : Int]()) { (dict, element) ->  [E : Int] in
            var dict = dict
            dict[element] = (dict[element] ?? 0) + 1
            return dict
        }
    }
}

(注意,闭包参数是常量,所以你必须做 首先是可变副本。闭包也必须返回一个值。)

但实际上你可以避免问题并让编译器推断出类型:

extension SequenceType where Self.Generator.Element : Hashable {
    func countRepetitions() -> [Self.Generator.Element : Int] {
        return self.reduce([:]) { (dict, element) in
            var dict = dict
            dict[element] = (dict[element] ?? 0) + 1
            return dict
        }
    }
}

另请注意,reduce在每次迭代中都会创建一个新字典 步。 更有效的解决方案是

extension SequenceType where Generator.Element : Hashable {
    func countRepetitions() -> [Generator.Element : Int] {
        var dict: [Generator.Element: Int] = [:]
        self.forEach {
            dict[$0] = (dict[$0] ?? 0) + 1
        }
        return dict
    }
}

我还省略了(冗余的)Self.