Skip to content

Instantly share code, notes, and snippets.

@Happytreat
Created May 13, 2020 04:56
Show Gist options
  • Save Happytreat/6f3847494162dbae7e2a6221a325abaa to your computer and use it in GitHub Desktop.
Save Happytreat/6f3847494162dbae7e2a6221a325abaa to your computer and use it in GitHub Desktop.
lost type - binarySearch
/**
* 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