Created
December 1, 2015 23:01
-
-
Save alskipp/67cc5a3661f635a62158 to your computer and use it in GitHub Desktop.
Example of the incompatibility between OO inheritance and protocol oriented Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
A silly example demonstrating how object-oriented inhertitance can conflict with protocol oriented Swift | |
*/ | |
class Animal { | |
func respire() { print("Yum, oxygen") } | |
} | |
class Wolf: Animal { | |
func growl() { print("Grrrr!") } | |
} | |
class Dog: Wolf { | |
func saySausages() { print("Sausages!") } | |
} | |
protocol Reproducible { | |
func reproduce(other: Self) -> Self | |
} | |
// The extension below will not compile. | |
// Non of our animals can conform to the Reproducible protocol. | |
// The Self requirement in the protocol requires that classes must be final (no OO inheritance) | |
extension Wolf: Reproducible { | |
func reproduce(other: Wolf) -> Wolf { | |
return Wolf() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment