0

Lets say we have the following struct which wraps around a closure:

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

What I would like is for the == function to return true if the closures are exactly the same.

I can then use this struct as the key to a dictionary declared as such: var dict = [Task, (Date, RateLimitedClosure)]

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Sabobin
  • 4,256
  • 4
  • 27
  • 33
  • Possible duplicate of http://stackoverflow.com/questions/24111984/how-do-you-test-functions-and-closures-for-equality – You *cannot* compare closures for equality. – Martin R Dec 10 '16 at 14:16

1 Answers1

0

There is no language construct that can do this.

I wonder if this question is even well defined. After all, A closure is really just a function pointer and the variables that it captures, right? Its easy enough to hash a function pointer, but its not so easy to derive hashes for all the arbitrary variables that the closure closes over.

Joe Daniels
  • 1,646
  • 1
  • 11
  • 9