0

假设我们有以下结构,它围绕一个闭包:

public struct Task: Hashable {

    pubic var closure: RateLimitedClosure

    public var hashValue: Int {
        // return unique hash
        return 1
    }

    public static func ==(lhs: Task, rhs: Task) -> Bool {
        return lhs.hashValue == rhs.hashValue
    }
}

==如果闭包完全相同,我想要函数返回true。

然后我可以使用这个结构作为字典的键,声明如下: var dict = [Task, (Date, RateLimitedClosure)]

4

1 回答 1

0

没有任何语言结构可以做到这一点。

我想知道这个问题是否定义得很好。毕竟,闭包实际上只是一个函数指针和它捕获的变量,对吧?散列一个函数指针很容易,但是为闭包关闭的所有任意变量派生散列并不容易。

于 2016-12-10T14:10:31.467 回答