12
protocol Car {
    static func foo() 
}

struct Truck : Car {

}

extension Car {
    static func foo() {
        print("bar")
    }
}

Car.foo() // Does not work  
// Error: Car does not have a member named foo

Truck.foo() // Works

Xcode autocompletes the Car.foo() correctly, so what i'm asking is if its a bug that it doesn't compile (says it does not have a member named foo()). Could you call static methods directly on the protocol if they are defined in a protocol extension?

hennes
  • 9,147
  • 4
  • 43
  • 63
bogen
  • 9,954
  • 9
  • 50
  • 89
  • 2
    Car is not an object so you can't send it any messages. – matt Aug 14 '15 at 09:11
  • This feels like a bug to me. Since the default implementation binds actual functionality directly to the protocol, it should be callable on the protocol itself. – hennes Aug 14 '15 at 11:49
  • @hennes "the default implementation binds actual functionality directly to the protocol" is not a true statement. The default implementation binds functionality to class and structs which conform to the protocol. There are [reasons](https://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem) protocols don't have implementations. – Jeffery Thomas Aug 14 '15 at 12:21
  • 2
    What seems like a bug is that Xcode actually suggest the autocomplete, if its not possible it should not do that – bogen Aug 14 '15 at 12:39

2 Answers2

9

Apple doc

Protocols do not actually implement any functionality themselves. Nonetheless, any protocol you create will become a fully-fledged type for use in your code.

Therefore, you cannot call static methods directly of protocol.

Community
  • 1
  • 1
Tikhonov Aleksandr
  • 13,945
  • 6
  • 39
  • 53
  • They can do it with a simple hack. Here is my related answer. https://stackoverflow.com/a/59275651/1163992 – qwerty Dec 11 '19 at 21:26
1

No, the error message isn't good, but it's telling you the correct thing.

Think of it this way, you can't have

protocol Car {
    static func foo() {
        print("bar")
    }
}

This compiles with the error "Protocol methods may not have bodies".

Protocol extensions don't add abilities to protocols which don't exist.

Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
  • In Swift 2 protocol extensions can actually add default implementations. – hennes Aug 14 '15 at 11:41
  • 1
    @hennes Yes, they add default implementations to classes and structs… not protocols. A protocol has no implementation. – Jeffery Thomas Aug 14 '15 at 11:45
  • [This](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-ID521) says otherwise, or am I mistaken? – hennes Aug 14 '15 at 11:47
  • 2
    @hennes [Protocols can be extended to provide method and property implementations to **conforming types**](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-ID521). The new protocol extension adds to a **class** or **struct** conforming to the protocol, but the protocol is still just a protocol. *Protocols don't have an implementation.* – Jeffery Thomas Aug 14 '15 at 12:00
  • 1
    So the point is that the default implementation is added to each conforming class/struct instead of the protocol. I see your point. Still, this feels a bit weird to me. – hennes Aug 14 '15 at 12:02