In addition to being able to specific generic types, as we saw in the previous recipe, you can use generics to build functions that are both widely applicable and strongly typed.
Using generics with functions
How to do it...
Let's use generics to build functions, and then we'll look at how they work:
- Let's imagine that we want to create a dictionary where the same value is inserted for multiple keys; we can use a generic function to create that functionality:
func makeDuplicates<ItemType>(of item: ItemType,
withKeys keys: Set<String>) -> [String: ItemType] {
var duplicates = [String: ItemType]()
for key in keys {
duplicates[key] = item
...