Discussion in 'iOS Programming' started by grandM, Dec 22, 2018.

  1. grandM macrumors 65816


    Oct 14, 2013
    The Apple documentation states one needs to implement the static function ==(lhs:rhs:). I do not understand why the function needed to be static. It aims at comparing instances anyway.
  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    The short answer is that's what 'protocol Equatable' requires. I believe this has changed from older versions of swift where operators were global functions. Having the func be static reinforces the fact that the comparison operator doesn't modify its parameters (but I'm not really sure that's why it is that way).
  3. grandM thread starter macrumors 65816


    Oct 14, 2013
    It still puzzles me. Being a static function implies you would code Struct == Struct which totally makes no sense. It is logical nevertheless as a static function is called on the structure itself like Struct.==

    This function is called between instances though. Struct() == Struct()
    Then again it can not imply it is Struct().== for this would not be a static function but an instance method.

    Thinking of it is Struct.==(lhs: Struct(), rhs: Struct())
  4. Mascots, Dec 27, 2018
    Last edited: Dec 27, 2018

    Mascots macrumors 68000


    Sep 5, 2009
    This evolution ticket may give you answers: https://github.com/apple/swift-evol...sals/0091-improving-operators-in-protocols.md

    It boils down to a combination of syntactic sugar to reduce inconsistencies within the language and compiler improvements by reducing scope clutter.

    The static keyword allows the compiler to use the existing universal lookup for an equatable call from a protocol as if it were any other method or function, satisfying the above issues without adding additional bloat.
  5. grandM thread starter macrumors 65816


    Oct 14, 2013

Share This Page