[swift-users] A confusing protocol extension behaviour/bug

Marc Palmer marc at anyware.co.uk
Tue Jan 2 06:56:10 CST 2018


> On 2 Jan 2018, at 09:12, Toni Suter <tonisuter at me.com> wrote:
> 
> 
> You can only 'overload' properties that are inherited from a protocol extension. 
> 
> protocol P {}
> extension P {
>     static var x: Int { return 2 }
> }
> struct S: P {
>     static var x = ""
> }
> 
> I am not exactly sure why this is allowed, but maybe someone from the Swift team can help.

FYI there’s a little more nuance to this. Even if `x` is defined as a requirement in the protocol, the compiler still doesn’t complain, and IMO this is the most heinous problem.

So it looks like:

1. If the property is declared in the protocol and something is marked as conforming to it, there is no excuse for permitting a property with the same name but wrong type

2. If the property is not declared in the protocol but only in an extension, not types should be allowed to define a property with the same name but different type as the property defined in the extension.

Marc



More information about the swift-users mailing list