Swift:调用 SequenceOf 的无参数扩展时缺少参数 [英] Swift: Missing argument in call to argumentless extension of SequenceOf

查看:27
本文介绍了Swift:调用 SequenceOf 的无参数扩展时缺少参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能看到这个错误吗?操场坚持认为缺少参数 #2,但没有参数 #1!

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.

我从调用下面的代码中得到的错误是在调用中缺少参数 #2 的参数

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())
                      ^

推荐答案

您遇到的问题是您实际上无法使用进一步专门化其泛型子类型的方法来扩展泛型类型.也就是说,你的 countRuns 方法要求 SequenceOf 的泛型子类型 TEquatable,但你可以仅在原始类型声明中提供这些约束,而不在扩展中提供.

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.

解决方案是将 countRuns 声明为顶级函数,如下所示:

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)))

这篇 NSHipster 文章的结尾处(一点点)介绍了这一点.

This was covered (a little) at the end of this NSHipster article.

这篇关于Swift:调用 SequenceOf 的无参数扩展时缺少参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆