cfisher cfisher - 3 months ago 7
Swift Question

Static keyword in Swift 3 protocol

I noticed that some functions in

Swift
protocols
have the
static
keyword. However, when you implement the function, you must remove the
static
keyword to make the compiler happy.

public static func <(lhs: Self, rhs: Self) -> Bool


What does
static
mean in the context and what is its purpose?

Answer

From the Xcode 8 beta 4 release notes:

Operators can be defined within types or extensions thereof. For example:

struct Foo: Equatable {
    let value: Int
    static func ==(lhs: Foo, rhs: Foo) -> Bool {
        return lhs.value == rhs.value
    }
}

Such operators must be declared as static (or, within a class, class final), and have the same signature as their global counterparts. As part of this change, operator requirements declared in protocols must also be explicitly declared static:

protocol Equatable {
    static func ==(lhs: Self, rhs: Self) -> Bool
}