• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Init in Swift
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Init in Swift


  • Subject: Re: Init in Swift
  • From: "Gerriet M. Denkmann" <email@hidden>
  • Date: Wed, 07 Sep 2016 11:14:12 +0700

> On 7 Sep 2016, at 10:49, Quincey Morris <email@hidden> 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 (email@hidden)

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:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Init in Swift
      • From: Quincey Morris <email@hidden>
References: 
 >Init in Swift (From: "Gerriet M. Denkmann" <email@hidden>)
 >Re: Init in Swift (From: Quincey Morris <email@hidden>)
 >Re: Init in Swift (From: "Gerriet M. Denkmann" <email@hidden>)
 >Re: Init in Swift (From: Quincey Morris <email@hidden>)

  • Prev by Date: Re: Security-scoped bookmarks, what's going on?
  • Next by Date: Re: Init in Swift
  • Previous by thread: Re: Init in Swift
  • Next by thread: Re: Init in Swift
  • Index(es):
    • Date
    • Thread