Skip to content

Instantly share code, notes, and snippets.

@rnapier
Created January 13, 2019 19:51
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 rnapier/e01a0e7a1e68317c4efea2732478df07 to your computer and use it in GitHub Desktop.
Save rnapier/e01a0e7a1e68317c4efea2732478df07 to your computer and use it in GitHub Desktop.
class Fruit {}
class Apple: Fruit {}
class Orange: Fruit {}
class Basket<T> where T: Fruit
{
var items: [T] = []
func add(_ item: T) {
items.append(item)
}
}
func addItem<T>(_ basket: Basket<T>, _ item: T) where T: Fruit
{
basket.add(item)
}
func logType(fruit: Fruit) {
print(type(of: fruit))
}
var basket = Basket<Fruit>()
addItem(basket, Apple())
addItem(basket, Orange())
basket.items.forEach(logType)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment