奇怪的编译器错误与斯威夫特协议从可切片的继承和添加单个属性
问题描述:
考虑下面的代码:奇怪的编译器错误与斯威夫特协议从可切片的继承和添加单个属性
protocol CollectionStreamType: Sliceable {
var position: Index { get set }
}
class Cell<T> {
let contents: T
init(_ contents: T) {
self.contents = contents
}
}
enum ParseResult<T, E> {
case Matched([T])
case NotMatched
case Err(Cell<E>)
}
func then<S: CollectionStreamType, T, E where S.SubSlice == S>(parsers: [S -> ParseResult<T, E>])(stream: S) -> ParseResult<T, E> {
let position = stream.position
// ERROR IS HERE: Type '()' does not conform to protocol 'GeneratorType'. WTH?
stream.position = position
return .NotMatched
}
我正在写一个解析器组合库斯威夫特叫吝啬。它运作良好,但我最近决定用协议CollectionStreamType
替换我的具体收集类型。当我这样做时,我开始有问题。
此代码不能直接从我的解析器组合库,但它提炼了问题的本质,并确实对我看到的错误,如下所示:
类型“()”不符合协议'GeneratorType'。
我感到困惑。任何人都有如何解决这个问题的见解?
答
不是最有用的编译器错误的,但我认为这是因为stream
是不可变的,所以你不能访问它的制定者。如果您制作stream
inout
或var
,或者如果您仅制作CollectionStreamType
,则编译此类。
我不知道我可以只做一个协议类。我怎么做? –
'Protocol CollectionStreamType:class,Sliceable {' –
你每天都会学到新的东西。这就是我要做的。 –