Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend existing protocols to implement another protocol with default implements

Tags:

Is it possible to add protocol compliance to a different protocol by way of an extension?

For instance we would like A to comply with B:

protocol A {   var a : UIView {get} }  protocol B {   var b : UIView {get} } 

I want to give a default implementation (compliance) of B to objects of type A

// This isn't possible extension A : B {   var b : UIView {     return self.a   } } 

The motivation being to reuse objects of A in cases where a B is required without creating my own "bridge"

class MyClass {   func myFunc(object : A) {     ...     ...     let view = object.a      ... do something with view ...      myFunc(object)      // would like to use an 'A' without creating a 'B'   }    func myFunc2(object : B) {     ...     ...     let view = object.b     ... do something with view ...    } } 

As a side note we can extend a class to implement a protocol

class C {   let C : UIView }  // this will work extension C : B {   var B : UIView {     return self.c   } } 

and protocols can give default implementations

extension A {   // a default implementation   var a : UIView {      return UIView()   } } 
like image 713
Avner Barr Avatar asked May 19 '16 14:05

Avner Barr


People also ask

Can a protocol extend another protocol?

Protocol extensions are different. You cannot “extend” a protocol because by definition a protocol doesn't have an implementation - so nothing to extend. (You could say that we “extend a protocol WITH some functionality”, but even an extended protocol is not something we can apply a function to.)

CAN protocol extend another protocol Swift?

You can extend an existing type to adopt and conform to a new protocol, even if you don't have access to the source code for the existing type. Extensions can add new properties, methods, and subscripts to an existing type, and are therefore able to add any requirements that a protocol may demand.

What are the protocols What is protocol extension?

Protocols let you describe what methods something should have, but don't provide the code inside. Extensions let you provide the code inside your methods, but only affect one data type – you can't add the method to lots of types at the same time.

Can a protocol conform to a protocol?

The answer is no. Protocols cannot conform to protocols. It may be easier to understand if existential protocol types were spelled differently from concrete types.


2 Answers

When extending A, you could specify that the type also conforms to B:

extension A where Self: B {     var b : UIView {         return self.a     } } 

Then make your type conform to A and B, e.g.

struct MyStruct : A, B {     var a : UIView {         return UIView()     } } 

Due to the protocol extension, instances of MyStruct will be able to use a and b, even though only a was implemented in MyStruct:

let obj = MyStruct() obj.a obj.b 
like image 165
ABakerSmith Avatar answered Sep 21 '22 15:09

ABakerSmith


You can make A inherits from B:

protocol A: B { var a: String { get } } protocol B    { var b: String { get } }  // Default implementation of property b extension A {     var b: String { get { return "PropertyB" } } }   class MyClass: A {     var a: String { get { return "PropertyA" } }      func printA(obj: A) {         print(obj.a)         printB(obj)     }      func printB(obj: B) {         print(obj.b)     } }  let obj = MyClass() obj.printA(obj) 

Since A inherits from B, every property in B is available in A.

like image 37
Code Different Avatar answered Sep 21 '22 15:09

Code Different