以下代码会生成细分错误:
extension Int
{
static prefix func++(i:Int)->Int{
return i+1;
}
static prefix func--(i:Int)->Int{
return i-1;
}
}
infix operator <-;
class DynamicList<T>: RangeReplaceableCollection, MutableCollection, BidirectionalCollection
{
var length:Int;
var arr:Array<T>?;
var startIndex:Int{
return 0;
}
var endIndex:Int{
return length;
}
subscript(i:Int)->T{
get{
return arr![i];
}
set{
arr![i] = newValue;
}
}
func index(after i: Int) -> Int{
return ++i;
}
func index(before i: Int) -> Int{
return --i;
}
required init(){
length = 0;
}
static func <- (left: inout DynamicList<T>, right: DynamicList<T>){
}
/* func replaceSubrange<C>(_ subrange: Range<Self.Index>, with newElements: C) where C : Collection,
C.Iterator.Element == Iterator.Element */
func replaceSubrange<C>(_ subrange: Range<DynamicList.Index>, with c: C)
where C : Collection, C.Iterator.Element == DynamicList.Iterator.Element{
}
}
我的目的是拥有通用类DynamicList<T>
采用三种协议:
class DynamicList<T>: RangeReplaceableCollection, MutableCollection, BidirectionalCollection
如上面的代码所示,但它会产生分段错误。
但是,当我用MutableCollection
替换Collection
时,上面的代码符合要求。
如果我删除BidirectionalCollection
,则上面的代码编译时没有分段错误。
不可能全部采用3:
同时?感谢。