【发布时间】:2016-03-08 13:49:50
【问题描述】:
我正在编写一个递归下降解析器。我希望我的解析器可以处理UInt8 的任何(或至少“许多”)集合(例如,不仅是 Swift.Array)
func unpack<T: CollectionType where T.Generator.Element == UInt8>(t: T) {
let m = t.dropFirst()
//[do actual parsing here]
unpack(m)
}
但是:
error: cannot invoke 'unpack' with an argument list of type '(T.SubSequence)'
note: expected an argument list of type '(T)'
这很令人费解,因为:
-
dropFirst返回Self.SubSequence -
CollectionType.SubSequence是SubSequence : Indexable, SequenceType = Slice<Self> -
Slice是CollectionType。 - 因此,
m应该是CollectionType。
但是由于某种原因,这不起作用。如何定义unpack 以便递归传递子序列?
【问题讨论】:
-
比较Recursion over a Swift Sliceable 的问题几乎相同。
-
其实是一样的问题,因为
Sliceable现在已经改名为CollectionType了。
标签: swift generics recursion types