Protocol declared in class

A thought regarding how protocols are used in Swift. Having the protocol(s) following the superclass class name using a comma is similar to the way multiple class inheritance is declared in C++.

C++ multiple inheritance:

Class myClass: superClassA, superClass B

Swift protocol used in a class:

class myClass: superClass, myProtocol

The two are similar. It would seem less confusing for someone coming from a C++ background if it was something like the following:

class myClass: superClass protocol: myProtocolA, myProtocolB


If there is a superclass in the inheritance, there can only be one and only one which must appear first in the inheritance clause. Additional markings wont help I am afraid. At worst you only ever have to check the first item in the inheritance to see if it is a protocol or a class.

protocol A {}
protocol B {}
class C {}
class SuperClass {}

class SomeClass: SuperClass, A, B {} // OK

class SomeClass2: C, A, B {} //Okay

class SomeClass3: A, B {} // Okay

class SomeClass3: A, B, SuperClass {} //error: superclass 'SuperClass' must appear first in the inheritance clause

Thanks Cheyo,

I understand how it works. It’s more about visual clarity and perhaps even aesthetics, like how Objective-C encloses protocol declarations in angle brackets.

It's also not possible to have a protocol and a class/struct share a common name because of the different (compared with Objective-C; guess this sentence can be confusing, because Swift has no difference ;-) syntax...

It's definitely a shame that implementing a protocol and inheriting from a class use the same syntax, doubly so when they're mixed together, but I think that ship has sailed! Might be nice if syntax colouring in Xcode showed a difference, though.