2

When i implement the Hashable protocol. a equatable protocol function is needed to be defined outside the class as follow. As follow.

func ==(lhs: Swap, rhs: Swap) -> Bool {
    return (lhs.cookieA == rhs.cookieA && lhs.cookieB == rhs.cookieB) ||
        (lhs.cookieB == rhs.cookieA && lhs.cookieA == rhs.cookieB)
}

class Swap: Printable,Hashable {
    var cookieA: Cookie
    var cookieB: Cookie

    init(cookieA: Cookie, cookieB: Cookie) {
        self.cookieA = cookieA
        self.cookieB = cookieB
    }
    var hashValue: Int {
    return cookieA.hashValue ^ cookieB.hashValue
    }

    var description: String {
    return "swap \(cookieA) with \(cookieB)"
    }
}

It is just a bit weird for me. In the above example func == should be belong to the class Swap. So why we need to declare the func == outside the class??

Siu Chung Chan
  • 1,686
  • 1
  • 14
  • 31
  • equatable is naturally a typeclass… and typeclass instances are defined outside of types. http://book.realworldhaskell.org/read/using-typeclasses.html – Display Name Oct 13 '14 at 18:03
  • [Here's a great explanation, along with a discussion how this may change in the future](http://stackoverflow.com/questions/35246003/why-must-a-protocol-operator-be-implemented-as-a-global-function). –  Mar 01 '16 at 03:10

3 Answers3

7

Mainly because it's a function and not a method. Functions are independent on classes so it makes no sense to scope them inside class declarations.

The protocol could have been designed with methods, e.g. using a obj1.isEqualTo(obj2) method but a function is less sensitive to nil problems. If obj1 were nil, the method would fail.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
1

I wouldn't read too much into it. I think it's just a simple design decision. Yes, in many languages operator overloads are methods on the left-hand-side operand and that's fine, but defining operators as functions probably gives you a bit more flexibility so they decided to make them all functions.

radex
  • 6,336
  • 4
  • 30
  • 39
1

The operator function is defined as a global function with a function name that matches the operator to be overloaded. The function is defined globally, rather than as a method on the target class or structure, so that it can be used as an infix operator between existing instances of the target class or structure.

I have answered this question in detail in my blog post here. Hope it helps.

jarora
  • 5,384
  • 2
  • 34
  • 46