Skip to content

Instantly share code, notes, and snippets.

@Happytreat
Created May 13, 2020 04:21
Show Gist options
  • Save Happytreat/ccefe91facf1690f18a1a7e174212815 to your computer and use it in GitHub Desktop.
Save Happytreat/ccefe91facf1690f18a1a7e174212815 to your computer and use it in GitHub Desktop.
lost type relationship - without Self
/**
* Example from: Protocol-oriented Programming in Swift - WWDC 2015
* https://developer.apple.com/videos/play/wwdc2015/408/
*/
class Ordered {
func precedes(other: Ordered) -> Bool { fatalError("implement me!") }
}
class Label : Ordered { var textL String = "" ... }
class Number: Ordered {
var value: Double = 0
override func precedes(other: Ordered) -> Bool {
// forced down-cast here is generally a code smell
// even though we can make this an optional down-cast, it is still a sign that some type relationship is lost
return value < (other as! Number).value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment