[swift-users] A confusing protocol extension behaviour/bug
Toni Suter
tonisuter at me.com
Tue Jan 2 07:06:30 CST 2018
Yes, I think I agree with you. Maybe we are missing something, but I don't
see a reason why declaring a property with the same name and a different type
should be allowed in the 1. scenario.
So maybe you should file a bug in JIRA, after all.
Best regards,
Toni
> Am 02.01.2018 um 13:56 schrieb Marc Palmer <marc at anyware.co.uk>:
>
>
>> 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