Skip to content

Instantly share code, notes, and snippets.

@tranhieutt
Created July 12, 2017 04:09
Show Gist options
  • Save tranhieutt/7933a19db16507eebcbf441b8a9b847e to your computer and use it in GitHub Desktop.
Save tranhieutt/7933a19db16507eebcbf441b8a9b847e to your computer and use it in GitHub Desktop.
func herd<Animal: Cat> (_ cats: [Animal]) {
cats.forEach {
print("Here,cat, \($0.name)! come here")
}
}
func herd<Animal: Dog> (_ dogs: [Animal]) {
dogs.forEach {
print("Here, dog, \($0.name)! come here")
}
}
// extension array
extension Array where Element: Cat {
func meow() {
forEach {
print(" \($0.name) says meows")
}
}
}
func herd<Animal: Pet> (_ animal: [Animal]) {
animal.forEach {
print("Animal here \($0.name)")
}
}
func herd<T:Cat, U:Dog>(_ cats:[T], _ dogs: [U]) {
cats.forEach {
print("Here cats: \($0.name)")
}
dogs.forEach {
print("Here dogs: \($0.name)")
}
}
herd(cats, dogs)
cats.meow()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment