What does this print?
protocol Box {
associatedtype Item
var item: Item { get }
}
struct IntBox: Box {
let item: Int
}
let b = IntBox(item: 9)
print(b.item * 2)
Try it in the online Swift Playground →
[spoiler title="Solution"]
Answer:
18
Explanation:
The Box protocol uses an associated type Item as a placeholder. IntBox conforms by setting Item = Int, making item a concrete Int property.
[/spoiler]