5

Coming from languages like Java/C++ we are used to partial or abstract class implementations e.g.

protocol ProtocolA {
   func x()
   func y()
}

// attempt to partially implement ProtocolA
class AbstractProtocolA: ProtocolA {
   func x() { /* implementation */ }

   // does not implement function y
}

class ConcreteA1: AbstractProtocolA {
   func y() { /* implementation */ }
}

class ConcreteA2: AbstractProtocolA {
   override func x() { /* implementation */ }
   func y() { /* implementation */ }
}

but this is not possible in Swift, in this case I would get the compilation error Type 'AbstractProtocolA' does not conform to protocol 'ProtocolA' ... is there another Swifty way to cover this OOD use-case?

UPDATE: A way to circumvent this limitation would be to:

enum ProtocolAError: ErrorType {
    case NotImplementedException(methodName: String)
}

class AbstractProtocolA: ProtocolA {
   func x() { /* implementation */ }

   func y() { throw ProtocolAError.NotImplementedException(methodName: "y") }
}

but here we shift design/programming mistakes (i.e. instantiating the abstract AbstractProtocolA) to runtime rather than compile time.

3
  • Have you looked at extensions? Commented May 30, 2016 at 7:44
  • Have you looked at the optional keyword? Commented May 30, 2016 at 7:45
  • 1
    No, I haven't looked ... write an answer don't be lazy :D Commented May 30, 2016 at 7:46

2 Answers 2

9

You can create a default implementation of the protocol with a protocol extension:

protocol ProtocolA {
   func x()
   func y()
}

extension ProtocolA {
   func x() {
      // Do something (or nothing) here as a default implementation
   }
}

Hope it works ;)

PS: have a look to protocol oriented programming in swift, here are some links: https://www.raywenderlich.com/109156/introducing-protocol-oriented-programming-in-swift-2 https://developer.apple.com/videos/play/wwdc2015/408/

Sign up to request clarification or add additional context in comments.

1 Comment

I think your answer is almost there ... to cover exactly what I wanted to achieve in my OP you would take out func y() from the extension otherwise it doesn't answer exactly :D
0

This at first looks like a bad design decision.
There are solutions like protocol extensions for default implementation, or the optional keyword, as posted here, but then you would have to make every method optional.
I think you should consider splitting the methods in different protocols.

3 Comments

I disagree, language limitations should not dictate the granularity/normalization level of your design ... you are in other words implying to end up with one-method-per-protocol design. I think extensions are the answer to my OP
@GiovanniAzua Protocols are used to ask the classes to follow a certain behaviour. It would be one thing to have a default implementation that actually does something. But having a default implementation that does nothing is misusing their role, and it should tell you that that class should probably not conform to that protocol or that method should be optional. It's just as you would add that empty method in a common parent and then override it in only one child.
I never wanted a default implementation that does nothing. The OP code was simply a mock sample of the use-case I need to cover. I don't know why you assume that ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.