Trying to eliminate code duplication I tried to use protocol extensions.
But I ran into a rather grave problem.
This probably is “working as documented”.
But it surely it is not working like I hoped it would.

Here is a Playground example:

protocol Number
{
        func aFunction() -> Int
}

extension Number
{
        func aFunction() -> Int
        {
                print("Error: \(self.dynamicType) must override \(#function)")
                return 0
        }
}

protocol Number1: Number {}

extension Number1
{
        func aFunction() -> Int
        {
                print("\(self.dynamicType) \(#function) returns 1")
                return 1
        }
}

// in my real code there are also a Number2 and Number3 protocols

class NewSuper: Number
{
        func superFunction() -> Int
        {
                return aFunction()
        }
}
class NewSubA: NewSuper
{
        func subAFunction() -> Int
        {
                return aFunction()
        }
}
class NewSubA1: NewSubA, Number1 {}

let newA1 = NewSubA1()

let c0 = newA1.aFunction()              //      ok
let c1 = newA1.superFunction()  //      prints: Error: NewSubA1 must override 
aFunction()
let c2 = newA1.subAFunction()   //      prints: Error: NewSubA1 must override 
aFunction()

Anything to be done here? Or should I just forget about protocol extensions?

Gerriet.


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to