泛型 – 在Swift中实现Set.addSequence

我在Swift中实现了一个使用Dictionary键的Set.我想实现一个addAll(序列)方法,该方法在Set中的Elements上采用任何序列类型,但是我得到一个没有意义的错误.这是我的代码
struct Set<Element: Hashable> {
    var hash = [Element: Bool]()

    init(elements: [Element] = []) {
        for element in elements {
            self.hash[element] = true
        }
    }

    var array: [Element] {
        return hash.keys.array
    }

    func contains(element: Element) -> Bool {
        return hash[element] ?? false
    }

    mutating func add(element: Element) {
        hash[element] = true
    }

    mutating func add(array: [Element]) {
        for element in array {
            hash[element] = true
        }
    }

    mutating func add<S : SequenceType where S.Generator.Element == Element>(sequence: S) {
        for element in sequence { // Error here: "Cannot convert the expression's type 'S' to type 'S'
            hash[element] = true
        }
    }

    mutating func remove(element: Element) {
        hash[element] = nil
    }
}

我在XCode 6.1和6.0.1中收到此错误.

我想遵循Array的extend方法的语义,但是这种类型的签名甚至都不能为我编译.

我做错了什么,还是应该提交雷达?

编辑:
刚刚找到https://github.com/robrix/Set/blob/master/Set/Set.swift,它有这个实现:

public mutating func extend<S : SequenceType where S.Generator.Element == Element>(sequence: S) {
    // Note that this should just be for each in sequence; this is working around a compiler crasher.
    for each in [Element](sequence) {
        insert(each)
    }
}

然而,这只是将序列转换为数组,这种类型完全违背了SequenceType的目的.

更新:这已经在Swift 1.2(Xcode 6.3 beta 3)中得到修复,问题的原始代码编译没有错误. (另外,定义
因为Swift 1.2具有自定义集类型,所以不再需要
内置的本机Set类型.)

老答案:对我来说这看起来像个错误,但也许有人可以解释它.

可能的解决方法:

>将sequence参数转换为SequenceOf< Element>明确:

mutating func add<S : SequenceType where S.Generator.Element == Element>(sequence: S) {
    for element in SequenceOf<Element>(sequence)  {
        hash[element] = true
    }
}

>(与https://stackoverflow.com/a/27181111/1187415相同)更换for循环
通过使用序列生成器的next()的while循环,并键入annotate
元素显式元素:元素:

mutating func add<S : SequenceType where S.Generator.Element == Element>(sequence: S) {
    var gen = sequence.generate()
    while let element : Element = gen.next() {
        hash[element] = true
    }
}

>(从“Creating a Set Type in Swift”开始)使用地图:

mutating func add<S : SequenceType where S.Generator.Element == Element>(sequence: S) {
    map(sequence) {
        self.hash[$0] = true
    }
}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...