Created
May 13, 2020 04:56
-
-
Save Happytreat/6f3847494162dbae7e2a6221a325abaa to your computer and use it in GitHub Desktop.
lost type - binarySearch
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
/** | |
* Adapted Example from: Protocol-oriented Programming in Swift - WWDC 2015 | |
* https://developer.apple.com/videos/play/wwdc2015/408/ | |
*/ | |
/** | |
* Without Self (protocol or class implementation) | |
* sortedKeys is a heterogeneous array of Ordered objects which could contain both Number and Label objects | |
* | |
* This implementation will raise an error if Self is being used: | |
* protocol `Ordered` can only be used as a generic constraint because it has Self or associated type requirements | |
*/ | |
func binarySearch(sortedKeys: [Ordered], forKey k: Ordered) -> Int { | |
... | |
} | |
/** | |
* With Self (protocol implementation) | |
* sortedKeys is a homogeneous array of any single Ordered type T enforced by the compiler | |
*/ | |
func binarySearch<T: Ordered>(sortedKeys: [T], forKey k: T) -> Int { | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment