• 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
Multiple Inheritance via protocol extensions in Swift
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Multiple Inheritance via protocol extensions in Swift


  • Subject: Multiple Inheritance via protocol extensions in Swift
  • From: "Gerriet M. Denkmann" <email@hidden>
  • Date: Wed, 07 Sep 2016 13:26:56 +0700

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 (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: Multiple Inheritance via protocol extensions in Swift
      • From: Quincey Morris <email@hidden>
  • Prev by Date: Re: Security-scoped bookmarks, what's going on?
  • Next by Date: Re: Multiple Inheritance via protocol extensions in Swift
  • Previous by thread: Re: Security-scoped bookmarks, what's going on?
  • Next by thread: Re: Multiple Inheritance via protocol extensions in Swift
  • Index(es):
    • Date
    • Thread