Roshan
Roshan

Reputation: 1937

Marking methods as final in protocol extensions

What are the implications of doing the following:

protocol A {
    func f()
}

extension A {
    final f() {}
}

I'm looking to understand what putting final in the extension over here does when compared to not putting it. I know what final does, I'm looking to understand overriding behaviour for classes implementing/not implementing f and their subclasses.

Upvotes: 4

Views: 1732

Answers (2)

SafeFastExpressive
SafeFastExpressive

Reputation: 3807

You can no longer mark extension funcs as final.

https://bugs.swift.org/browse/SR-1762

Upvotes: 3

Abhijeet
Abhijeet

Reputation: 8771

For Swift 2.0: Protocol Extensions

Methods, properties, or subscripts that you add to a class in an extension can also be marked as final within the extension’s definition.

Referring to this question of Inheritance, final keyword would not allow to override the (final) method defined in Extension, when it's Sub-classed.

For Swift 1.2: This might be useful in context of Extensions Extensions can add new functionality to a type, but they cannot override existing functionality

Few things to look in context of Extensions, while decide on architecture of applications:

  1. Protocol's cannot be extended to Extension.
  2. It has go through a Class.
  3. Existing functionality cannot be overridden (invalid redeclaration of)

On final keyword, this is my take it has no impact on Extensions methods as compared to classes(Inheritance). Please correct me if I'm wrong here. Demo links:

  1. Protocol A-->Class B-->Extension B
  2. Protocol A-->Class B-->Extension B, then Class B --> Class C

Upvotes: 1

Related Questions