Created
June 7, 2014 14:38
-
-
Save stollcri/871bfcb91576174e11d8 to your computer and use it in GitHub Desktop.
Protocol type constraints for generics in Apple Swift
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
/* | |
* Here the Comparable protocol is used as the type constraint | |
* For equality testing the Equitable protocol would be needed | |
* To print a variable it must conform to the Printable protocol | |
* | |
* see also: | |
* https://developer.apple.com/library/prerelease/ios/documentation/General/Reference/SwiftStandardLibraryReference/Equatable.html#//apple_ref/doc/uid/TP40014608-CH17-SW1 | |
*/ | |
func min3a_generics<T: Comparable>(a: T, b: T, c: T) -> T { | |
if a < b { | |
if a < c { | |
return a | |
} else { | |
return c | |
} | |
} else { | |
if b < c { | |
return b | |
} else { | |
return c | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment