Created
May 13, 2020 04:33
-
-
Save Happytreat/311251c3923d164231cd495f8afd710c to your computer and use it in GitHub Desktop.
lost type relationships - with Self
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
/** | |
* Example from: Protocol-oriented Programming in Swift - WWDC 2015 | |
* https://developer.apple.com/videos/play/wwdc2015/408/ | |
*/ | |
protocol Ordered { | |
func precedes(other: Self) -> Bool | |
} | |
// value type struct | |
struct Number: Ordered { | |
var value: Double = 0 | |
// we can now use Number: no more lost type | |
override func precedes(other: Number) -> Bool { | |
return self.value < other.value | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment