Skip to content

Instantly share code, notes, and snippets.

@stollcri
Created June 7, 2014 14:38
Show Gist options
  • Save stollcri/871bfcb91576174e11d8 to your computer and use it in GitHub Desktop.
Save stollcri/871bfcb91576174e11d8 to your computer and use it in GitHub Desktop.
Protocol type constraints for generics in Apple Swift
/*
* 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