Created
January 13, 2019 19:51
-
-
Save rnapier/e01a0e7a1e68317c4efea2732478df07 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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