Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.