Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why must a protocol operator be implemented as a global function?

People also ask

What is protocol and how do you implement it?

A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements.

How do you implement a protocol?

Protocols are basically set of rules. The way to implement them is to first of all make a state machine diagram as it completely tells that what is going to be the current state and how the state is going to change on the basis of input and what output actions are going to be performed.

Why do we use protocols in Swift?

Swift checks for protocol conformity issues at compile-time, allowing developers to discover some fatal bugs in the code even before running the program. Protocols allow developers to write flexible and extensible code in Swift without having to compromise the language's expressiveness.

Can a protocol be Equatable Swift?

In Swift, an Equatable is a protocol that allows two objects to be compared using the == operator. The hashValue is used to compare two instances. To use the hashValue , we first have to conform (associate) the type (struct, class, etc) to Hashable property.


UPDATE

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
}

ORIGINAL

This was discussed on the swift-evolution list recently (2016-01-31 through 2016-02-09 so far). Here's what Chris Lattner said, regarding declaring operators in a struct or class scope:

Yep, this is a generally desirable feature (at least for symmetric operators). This would also be great to get dynamic dispatch of operators within class declarations. I don’t think we have a firm proposal nailing down how name lookup works with this though.

And later (replying to Haravikk):

What are the name lookup issues? Do you mean cases where an operator for Foo == Foo exists in more than one location?

Yes. Name lookup has to have a well defined search order, which defines shadowing and invalid multiple definition rules.

Personally I’d just stick with what we have now, i.e- treat operator implementations within a specific class/struct as being globally defined anyway and throw an error if the same signature is declared more than once.

We need multiple modules to be able to define instances of an operator, we need operators in extensions, and we need retroactive conformance to work, as with any other member.


Explanation from the documentation

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 replaced the name of the concrete structure (Vector2D) in the quotation by a generic expression target class or structure


The fact that its implementation needs to be declared at a global scope, makes it seem incidental to and distinct from a protocol, even if Equatable was adopted.

That is true of every protocol, whether it requires global functions, class methods, or instance methods. You can always implement things independently of whether there is a protocol that is requiring it.

How is the Equatable protocol anything more than syntactic sugar that merely lets (us and) the compiler safely know that our type implemented the required method of the protocol?

That's not sugar. That's the definition of a protocol and the entire point of protocols. It tells you that this type has available these things that can be applied to it.

Why does the operator implementation have to be globally declared, even for a protocol? Is this due to some different way that an operator is dispatched?

Because that's Swift syntax. Operator function implementations have to be global functions. There has been interest from the Swift team to change this to make it more consistent, but today this is just how Swift works.


It was somewhat of a sugar before ie because you needed to manually do the implementation of Equatable. Though Rob Napier brought up a good point that that's what protocols are for.

But that's no longer the case ie just by conformance you get Automated Synthesis and no longer need the boilerplate code.

struct Country: Equatable {
  let name: String
  let capital: String
  var visited: Bool
}

That’s it! The compiler will do the rest.

let france = Country(name: "France", capital: "Paris", visited: true)
let spain = Country(name: "Spain", capital: "Madrid", visited: true)
if france == spain { ... } // false

That being said if you want you can still override the implementation of the == function.

For more see here