问题描述
任何人都可以看到这个bug光?操场坚持这样的说法#2丢失,但没有说法#1!
在code的意图来算一个equatable值运行的次数,并返回组成的价值观和他们的计数的元组序列。我对这个code广泛合作,优化并完善它,直到我pretty确保它应该工作......不过虽然它编译,我不能称之为它的目的的方式。
我从呼叫低于code得到的错误是缺少呼吁参数#2参数
扩展SequenceOf {
FUNC CountRuns< T:Equatable>() - GT; SequenceOf≤(T,智力)> {
返回SequenceOf≤(T,智力)>([])
返回SequenceOf≤(T,智力)> {() - > GeneratorOf≤(T,智力)>在
无功发生器= self.generate()
VAR previousValue:T'
VAR启动= TRUE
返回GeneratorOf≤(T,智力)> {() - > (T,智力)?在
变种数= 1
VAR retValue:(T,智力)?
而(真){
VAR值= generator.next()为T?
如果启动{
previousValue =价值
启动=假
}否则,如果值=零和放大器;!&安培;值! == previousValue! {
算上++
}其他{
如果previousValue!= {为零
retValue =(previousValue!计数)
}
previousValue =价值
打破
}
}
返回retValue
}
}
}
}的println(SequenceOf(Y).CountRuns())游乐场执行失败:其中,EXPR>:327:23:错误:呼叫参数#2缺少参数
的println(SequenceOf(Y).CountRuns())
^
您遇到的问题是,你不能真正以进一步专注其仿制亚型的方法扩展泛型类型。也就是说,你的 countRuns
方法要求 SequenceOf
的通用亚型 T
是 Equatable
,但你只能在原来的类型声明提供这些约束,不能延期。
解决方案是申报 countRuns
作为一个顶级功能,像这样:
FUNC countRuns< T:Equatable>(S:SequenceOf< T>) - GT; SequenceOf≤(T,智力)> {
返回SequenceOf≤(T,智力)> {() - > GeneratorOf≤(T,智力)>在 //注意,从self.generate()的变化来s.generate()这里
无功发生器= s.generate() VAR previousValue:T'
VAR启动= TRUE
返回GeneratorOf≤(T,智力)> {() - > (T,智力)?在
变种数= 1
VAR retValue:(T,智力)?
而(真){
VAR值= generator.next()为T?
如果启动{
previousValue =价值
启动=假
}否则,如果值=零和放大器;!&安培;值! == previousValue! {
算上++
}其他{
如果previousValue!= {为零
retValue =(previousValue!计数)
}
previousValue =价值
打破
}
}
返回retValue
}
}
}的println(countRuns(SequenceOf(Y)))
这是在覆盖(一点点)。
Can anybody see light on this bug? The playground insists that argument #2 is missing, but there is no argument #1!
The intention of the code to to count the number of runs of a equatable value, and return a sequence of tuples consisting of the values and their counts. I've worked on this code extensively, optimising it and refining it until I'm pretty sure that it should work… but although it compiles, I cannot call it the way it was intended.
The error i get from calling the code below is missing argument for parameter #2 in call
extension SequenceOf {
func CountRuns<T: Equatable>() -> SequenceOf<(T, Int)> {
return SequenceOf<(T, Int)>([])
return SequenceOf<(T, Int)> { () -> GeneratorOf<(T, Int)> in
var generator = self.generate()
var previousValue: T?
var start = true
return GeneratorOf<(T, Int)> { () -> (T, Int)? in
var count = 1
var retValue: (T, Int)?
while(true) {
var value = generator.next() as T?
if start {
previousValue = value
start = false
} else if value != nil && value! == previousValue! {
count++
} else {
if previousValue != nil {
retValue = (previousValue!, count)
}
previousValue = value
break
}
}
return retValue
}
}
}
}
println(SequenceOf(y).CountRuns())
Playground execution failed: <EXPR>:327:23: error: missing argument for parameter #2 in call
println(SequenceOf(y).CountRuns())
^
The problem you're having is that you can't actually extend a generic type with a method that further specializes its generic subtype. That is to say, your countRuns
method requires that SequenceOf
's generic subtype T
be Equatable
, but you can only provide those constraints in the original type declaration, not in an extension.
The solution is to declare countRuns
as a top-level function, like so:
func countRuns<T: Equatable>(s: SequenceOf<T>) -> SequenceOf<(T, Int)> {
return SequenceOf<(T, Int)> { () -> GeneratorOf<(T, Int)> in
// note the change from self.generate() to s.generate() here
var generator = s.generate()
var previousValue: T?
var start = true
return GeneratorOf<(T, Int)> { () -> (T, Int)? in
var count = 1
var retValue: (T, Int)?
while(true) {
var value = generator.next() as T?
if start {
previousValue = value
start = false
} else if value != nil && value! == previousValue! {
count++
} else {
if previousValue != nil {
retValue = (previousValue!, count)
}
previousValue = value
break
}
}
return retValue
}
}
}
println(countRuns(SequenceOf(y)))
This was covered (a little) at the end of this NSHipster article.
这篇关于斯威夫特:呼叫缺少参数argumentless SequenceOf延伸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!