[swift-evolution] [Pitch] separate syntax of class inheritance and protocol conformancemo

Duan daniel at duan.org
Fri Jul 22 12:01:51 CDT 2016



Daniel Duan
Sent from my iPhone

> On Jul 22, 2016, at 9:52 AM, Vladimir.S <svabox at gmail.com> wrote:
> 
>> On 22.07.2016 19:25, Duan wrote:
>> The current syntax establishes a relationship.
>> 
>>  X: Y
>> 
>> means "X will have interface of Y". Note there's no mention of inheritance or protocol here: they both establish this relationship. This is why we can use
>> 
>>  z is Y
>> 
>> To check whether such relationship holds. Again, there's no need to worry whether Y is a class or protocol.
> 
> But we see that some mature languages like Java, C# and C++, Object Pascal has this distinction: Java with 'implements' keyword, C# with 'I' prefix for interfaces(their protocols), C++ with both `implements` and 'I' prefix conversion, Object Pascal with 'I' prefix. I.e. I believe that in any case new syntax will establish a relationship, but more clearly distinct inheritance with conformance to help the reader to understand the composition of the reviewed class code.
> 

I argue these syntax are ugly and unnecessary. Reason is in my initial reply.

>> 
>> That being said, I agree that user have to look up Y's declaration to determine which one they are writing/reading. But is that a real issue? Why would you write/use "class X: Y {}" before knowing what Y is?
> 
> The problem arises not when you write/use but when you *read* someone's code, and sometimes you read that code not in XCode/IDE.
> 

I repeat: when you read X: Y, you would not understand what X is without knowing about Y.

> 
>> 
>> Daniel Duan
>> Sent from my iPhone
>> 
>>> On Jul 22, 2016, at 6:14 AM, Vladimir.S via swift-evolution <swift-evolution at swift.org> wrote:
>>> 
>>> I remember that this was discussed, but can't find any decision regarding this.. So, as a last chance, don't we want in Swift 3.0, as big source breaking change, separate class inheritance and protocol conformance in syntax?
>>> 
>>> Sorry if there was a decision about this suggestions. Please let know in this case.
>>> 
>>> I.e. when I see the following I can't understand if the class inherits from base class and conforms to protocols or just conforms to two protocols:
>>> 
>>> class MyClass : First, Second, Third {
>>> }
>>> 
>>> We don't have a rule to name protocols with 'Protocol'/other suffix/prefix, or classes with 'T'/'C' prefix or something like this, so I believe to improve the clarity of code we should separate in syntax inheritance and conformance.
>>> 
>>> As I understand we should discuss changes in these areas:
>>> 
>>> 1. class inheritance :
>>> class Child: BaseClass
>>> 
>>> 2. class conformance :
>>> class Child: SomeProtocol1, SomeProtocol2
>>> 
>>> 3. class inheritance + conformance :
>>> class Child: BaseClass, SomeProtocol1, SomeProtocol2
>>> 
>>> 4. protocol conformance for structs:
>>> struct Struct: SomeProtocol1, SomeProtocol2
>>> 
>>> 5. protocol inheritance:
>>> protocol Child: BaseProtocol1, BaseProtocol2
>>> 
>>> 
>>> My suggestions:
>>> 
>>> I) separate inheritance with double colon :
>>> 
>>> 1. class inheritance :
>>> class Child:: BaseClass
>>> 
>>> 2. class conformance :
>>> class Child: SomeProtocol1, SomeProtocol2
>>> 
>>> 3. class inheritance + conformance :
>>> class Child:: BaseClass : SomeProtocol1, SomeProtocol2
>>> 
>>> 4. protocol conformance for structs:
>>> struct Struct: SomeProtocol1, SomeProtocol2
>>> 
>>> 5. protocol inheritance:
>>> protocol Child:: BaseProtocol1, BaseProtocol2
>>> 
>>> 
>>> II) in class definition use parenthesis to separate inheritance and conformance :
>>> 
>>> 1. class inheritance :
>>> class Child: BaseClass
>>> 
>>> 2. class conformance :
>>> class Child: (SomeProtocol1, SomeProtocol2)
>>> 
>>> 3. class inheritance + conformance :
>>> class Child: BaseClass (SomeProtocol1, SomeProtocol2)
>>> 
>>> 4. protocol conformance for structs:
>>> struct Struct: SomeProtocol1, SomeProtocol2
>>> or
>>> struct Struct: (SomeProtocol1, SomeProtocol2)
>>> should be discussed
>>> 
>>> 5. protocol inheritance:
>>> protocol Child: BaseProtocol1, BaseProtocol2
>>> 
>>> 
>>> III) special word like 'conforms'
>>> 
>>> 1. class inheritance :
>>> class Child: BaseClass
>>> 
>>> 2. class conformance :
>>> class Child: conforms SomeProtocol1, SomeProtocol2
>>> or
>>> class Child conforms SomeProtocol1, SomeProtocol2
>>> 
>>> 3. class inheritance + conformance :
>>> class Child: BaseClass conforms SomeProtocol1, SomeProtocol2
>>> 
>>> 4. protocol conformance for structs:
>>> struct Struct: conforms SomeProtocol1, SomeProtocol2
>>> or
>>> struct Struct conforms SomeProtocol1, SomeProtocol2
>>> 
>>> 5. protocol inheritance:
>>> protocol Child: BaseProtocol1, BaseProtocol2
>>> 
>>> 
>>> Thoughts?
>>> _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>> 
>> 



More information about the swift-evolution mailing list