Skip to content

Instantly share code, notes, and snippets.

@laevandus
Created August 2, 2018 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laevandus/7ff6840beebd167f2bccf1dbf2dcfe67 to your computer and use it in GitHub Desktop.
Save laevandus/7ff6840beebd167f2bccf1dbf2dcfe67 to your computer and use it in GitHub Desktop.
// 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