kodecocodes/swift-style-guide

Can you explain more about this sentence ?

SwiftSIQI opened this issue · 5 comments

In Protocol Conformance Section

Since the compiler does not allow you to re-declare protocol conformance in a derived class, it is not always required to replicate the extension groups of the base class. This is especially true if the derived class is a terminal class and a small number of methods are being overridden.

  1. "it is not always required to replicate the extension groups of the base class" , This sentence is hard to understand, I don't know what is "replicate" mean in code , Could you give me an example?
  2. What is "This" mean in "This is especially true if....", and what's the connection between with the first sentence ?

Thank you very much

Hi SketchK! Let me give answering your question a shot. Perhaps an example for each may suffice.

  1. I think this is in reference to providing additional protocol conformance via extensions.
class Shape {
  let numberOfSides: Int
    
  init(numberOfSides: Int) {
    self.numberOfSides = numberOfSides
  }
}

extension Shape: CustomStringConvertible {
  var description: String {
    return "A shape with \(numberOfSides) sides"
  }
}

class Triangle: Shape {
  init() {
    super.init(numberOfSides: 3)
  }

  // ...
}

let square = Shape(numberOfSides: 4)
print(square) // "A shape with 4 sides"

let triangle = Triangle()
print(triangle) // "A shape with 3 sides"

Here, we did not have to replicate the CustomStringConvertible conformance, as it is inherited from the base Shape class.

  1. "This", I believe, is in reference to not having to replicate the extension groups. I think the purpose of this section is to make note that derived classes can be much smaller in size related to the base class, if the base class defines additional protocol conformance(s).

@dskuza ,Thank you very much, It helps me a lot.
but the last sentence “a small number of methods are being overridden” still confuse me?
Does it mean some methods is implemented by base class ,so you don't need to implement again in derived?

@SketchK I think it's meant to be taken literally from an example - a terminal class (i.e one that completes implementation from a base class, I believe) can commonly require few methods to be overridden, as the base class perhaps implemented common functionality for > 1 derived class.

@dskuza Thank you for your patience !
I read your answer and the original sentence again and again,

I think this style guide want to tell us that : when terminal class has the same behavior as base class, we can use extension to organize our code, but if some method needs to be overridden, we can't write method in extension, because compiler will send warning.

Is this correct?

@SketchK No, that's not correct. You can absolutely write the method in an extension. What you can't do is declare that the extension conforms to the protocol. The easiest example is UITableViewController. It already conforms to UITableViewDelegate and UITableViewDataSource. Normally, the style guide would have you put those protocol conformances into extensions. However, if your class subclasses UITableViewController, it already has declared conformance to those protocols and you cannot redeclare it. This leaves you with two options: put the relevant methods inside the main class declaration or (this is my preference) put them in an extension and use a // MARK: - comment to indicate the protocol grouping since you can't declare it on the extension itself.