Skip to content

Instantly share code, notes, and snippets.

@alskipp
Created December 1, 2015 23:01
Show Gist options
  • Save alskipp/67cc5a3661f635a62158 to your computer and use it in GitHub Desktop.
Save alskipp/67cc5a3661f635a62158 to your computer and use it in GitHub Desktop.
Example of the incompatibility between OO inheritance and protocol oriented Swift
/*
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