Generics 在Swift中实现Set.addSequence
我在Swift中实现了一个使用字典键的集合。我想实现一个addAll(sequence)方法,该方法对集合中的元素采用任何序列类型,但是我得到了一个没有意义的错误。这是我的密码Generics 在Swift中实现Set.addSequence,generics,swift,Generics,Swift,我在Swift中实现了一个使用字典键的集合。我想实现一个addAll(sequence)方法,该方法对集合中的元素采用任何序列类型,但是我得到了一个没有意义的错误。这是我的密码 struct Set<Element: Hashable> { var hash = [Element: Bool]() init(elements: [Element] = []) { for element in elements { self.h
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
}
}
结构集{
var hash=[Element:Bool]()
初始化(元素:[元素]=[]){
对于元素中的元素{
self.hash[element]=true
}
}
变量数组:[元素]{
返回hash.keys.array
}
func包含(元素:元素)->Bool{
返回哈希值[元素]??false
}
变异函数添加(元素:元素){
哈希[元素]=真
}
变异函数添加(数组:[元素]){
对于数组中的元素{
哈希[元素]=真
}
}
变异函数添加(序列:S){
对于序列{//中的元素,此处出现错误:“无法将表达式的类型“s”转换为类型“s”
哈希[元素]=真
}
}
变异函数删除(元素:元素){
哈希[元素]=nil
}
}
我在XCode 6.1和6.0.1中遇到了这个错误
我想遵循数组的extend方法的语义,但该类型签名甚至不能为我编译
我是做错了什么事,还是应该申请雷达
编辑:
刚刚发现,它有以下实现:
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)
}
}
public mutating func extend(序列:S){
//请注意,这应该只针对序列中的每一个;这是针对编译器崩溃器的工作。
对于[元素]中的每个元素(序列){
插入(每个)
}
}
然而,这只是将序列
转换为数组
,这完全违背了序列类型
的目的。更新:这在Swift 1.2(Xcode 6.3 beta 3)中得到了修复,问题中的原始代码编译没有错误。(另外,定义
由于Swift 1.2具有
本机集
内置类型。)
老答案:在我看来,它像一只虫子,但也许有人能解释它 可能的解决办法:
- 将
参数显式转换为sequence
:SequenceOf
mutating func add<S : SequenceType where S.Generator.Element == Element>(sequence: S) { for element in SequenceOf<Element>(sequence) { hash[element] = true } }
- (来自)使用
地图
:
mutating func add<S : SequenceType where S.Generator.Element == Element>(sequence: S) { map(sequence) { self.hash[$0] = true } }
mutating func add(序列:S){ 地图(序列){ self.hash[$0]=true } }
mutating func add<S : SequenceType where S.Generator.Element == Element>(sequence: S) {
var generator = sequence.generate()
while let item = generator.next() {
self.hash[item] = true
}
}
使用reduce而不是map可能更为理想,因为这不会构建要丢弃的数组:
mutating func add<S : SequenceType where S.Generator.Element == Element>(sequence: S) {
reduce(sequence, ()) {
self.hash[$1] = true
}
}
mutating func add(序列:S){
减少(顺序,()){
self.hash[$1]=true
}
}
您的第一个变体可以在let item:Element=generator.next(){..}的时候使用。使用reduce()
是个好主意(但是map/reduce都只是黑客,in的一个简单应该可以使用。)该错误消息是一个进入bug的窗口-for…in
实际上只是调用generate()
,所以那里的错误很可能是阻止了的原因。@MartinR同意,只是在那里抛出了更多的信息和选项。肯定是OP应该发布到bugs.apple.com的东西。我同意这是一个bug。有人报告了吗?谢谢you@drasto当前位置我不知道。但无论如何,如果你受此影响问题,提交你自己的bug报告。使用map
似乎是最好的解决方法。我想主要的问题是map
确实不允许有副作用。
Cannot convert the expression's type '()' to type 'Self.Element??'
mutating func add<S : SequenceType where S.Generator.Element == Element>(sequence: S) {
reduce(sequence, ()) {
self.hash[$1] = true
}
}