Created
August 2, 2018 05:04
-
-
Save laevandus/7ff6840beebd167f2bccf1dbf2dcfe67 to your computer and use it in GitHub Desktop.
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
// non-generic functions | |
func someFunctionInt(_ values: [Int]) {} | |
func someFunctionString(_ values: [String]) {} | |
// generic | |
func someFunction<T>(_ values: [T]) {} | |
// non-generic functions | |
someFunctionInt([1, 2, 3]) | |
someFunctionString(["a", "b", "c"]) | |
// generic | |
someFunction([1, 2, 3]) | |
someFunction(["a", "b", "c"]) | |
// Type constraint | |
func someSortFunction<T: Comparable>(_ values: [T]) -> [T] { | |
return values.sorted() | |
} | |
let sorted = someSortFunction([3, 7, 5, 1]) | |
print(sorted) // [1, 3, 5, 7] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment