> On 7 Sep 2016, at 10:49, Quincey Morris <quinceymor...@rivergatesoftware.com> 
> wrote:
> 
>> Another problem:
>> Super → SubA and SubB.
>> SubA → SubA1 and SubA2
>> SubB → SubB1 and SubB2
>> 
>> Both SubA1 and SubB1 have identical functions. As have  SubA2 and SubB2.
>> Multiple inheritance would be a solution here; but neither Objective-C nor 
>> Swift can do this (and I don’t like C++).
> 
> There’s not necessarily a straightforward conversion from subclassing to 
> protocol composition. It usually takes some fundamental re-thinking of your 
> approach to to the problem.

Following a suggestion from Greg Parker I solved my multiple inheritance 
problem with protocol extensions:

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
        }
}

[… Omitted similar code with “1” replaced by “2” …]

class NewSuper: Number {}
class NewSubA: NewSuper {}
class NewSubB: NewSuper {}
class NewSubA1: NewSubA, Number1 {}
class NewSubA2: NewSubA, Number2 {}
class NewSubB1: NewSubB, Number1 {}
class NewSubB2: NewSubB, Number2 {}

No more code repetition here. Very satisfying so far. Thanks to Greg Parker!

But now NewSuper has a class function like:
        class func classFunction( number: UInt64 ) -> UInt64    
        {
                print(“Error:  must override \(#function)")
                return 0
        } 
which is overridden in NewSubA1,2  NewSubB1,2 in the same style as aFunction.
So it should be part of my Number… protocols and extensions.
But I cannot get this to work.

NewSuper uses this classFunction like:
func otherFunction( arg: UInt64) -> UInt64
{
        return self.dynamicType.classFunction(  arg ) + 21
}

Kind regards,

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