在Swift中实现Set.addSequence [英] Implementing Set.addSequence in Swift
问题描述
我在Swift中实现了一个使用Dictionary键的Set。我想实现一个addAll(sequence)方法,它可以在Set中的元素上使用任何序列类型,但是我得到一个没有道理的错误。这是我的代码
struct Set< Element:Hashable> {元素:Bool]()
$ b $元素{元素:[元素] = []){$ b $元素中的元素{
self。 hash [element] = true
}
}
var array:[Element] {
return hash.keys.array
}
func包含(element:Element) - > Bool {
return hash [element] ?? false
mutating func add(element:Element){
hash [element] = true
}
mutating func add( array:[元素]){
用于数组中的元素{
hash [元素] = true
}
}
mutating func add< S: SequenceType其中S.Generator.Element ==元素>(序列:S){
用于元素的顺序{//错误在这里:无法将表达式的类型'S'转换为类型'S'
hash [element] = true
}
}
mutating func remove(element:Element){
hash [element] = nil
}
}
在XCode 6.1和6.0.1中出现这个错误。
我想遵循Array的扩展方法的语义,但是这种类型签名甚至没有为我编译。
我是做错了什么,或者我应该提交一份雷达?
编辑:
刚刚找到 https://github.com/robrix/Set/blob/master /Set/Set.swift ,它有这个实现:
public mutating func extend< S:SequenceType其中S .Generator.Element == Element>(sequence:S){
//注意,这只适用于每个序列;这是围绕着一个编译器问题。
insert(each)
}
}
但是,它只是将 sequence
转换为 Array
,这种类型的打破了 SequenceType
的目的。 更新:这已经在Swift 1.2(Xcode 6.3 beta 3)中修复,原始版本来自问题的代码编译没有错误。 (另外,定义
a自定义类型不再是必要的,因为Swift 1.2有
a原生 Set
类型的内置。)
老回答:对我来说这看起来像一个bug,但也许有人可以解释它。 b
$ b
可能的解决方法: 转换 /stackoverflow.com/a/27181111/1187415\">https://stackoverflow.com/a/27181111/1187415 )使用 (From map : I've implemented a Set in Swift that uses Dictionary keys. I want to implement an addAll(sequence) method that takes any sequence type over the Elements in the Set, but I'm getting an error that doesn't make sense. Here's my code I'm getting this error in XCode 6.1 and 6.0.1. I wanted to follow the semantics of Array's extend method, but that type signature doesn't even compile for me. Am I doing something wrong, or should I file a Radar? edit:
just found https://github.com/robrix/Set/blob/master/Set/Set.swift, which has this implementation: However, that just converts Update: This has been fixed in Swift 1.2 (Xcode 6.3 beta 3), the original code from the question compiles without errors. (Also, defining
a custom set type is not necessary anymore because Swift 1.2 has
a native Old answer: It looks like a bug to me, but perhaps someone can explain it. Possible workarounds: Convert the (As in https://stackoverflow.com/a/27181111/1187415) Replace the for-loop
by a while-loop using (From "Creating a Set Type in Swift") Use 这篇关于在Swift中实现Set.addSequence的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
序列$
SequenceOf< Element>
明确显示:
变异func add< S:SequenceType其中S.Generator.Element == Element>(sequence:S){
for SequenceOf< Element>(sequence){
hash [element] = true
}
> next(),然后使用
元素显式注释
:
元素:Element
<$ p: 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
}
}
mutating func add< S:SequenceType where S.Generator.Element == Element>(sequence:S){
map(sequence){
self.hash [$ 0] =
$ b $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
}
}
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)
}
}
sequence
into an Array
, which kind of defeats the purpose of SequenceType
altogether. Set
type built-in.)
sequence
argument to SequenceOf<Element>
explicitly:mutating func add<S : SequenceType where S.Generator.Element == Element>(sequence: S) {
for element in SequenceOf<Element>(sequence) {
hash[element] = true
}
}
next()
of the sequence generator, and type annotate
the element explicitly with element : Element
: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
}
}
map
:mutating func add<S : SequenceType where S.Generator.Element == Element>(sequence: S) {
map(sequence) {
self.hash[$0] = true
}
}