Skip to content

Instantly share code, notes, and snippets.

//: #typealias & associatedtype magic
//: ##Let's make a protocol without typealias (now associatedtype)
protocol Testable {
}
//: ##And another one with typealias
//: You could think, that the below typealias is just making
//: a typealiast for Int. In fact, we actually created a
let currencies = [CurrencyProtocol]()
struct ReallyImportantCurrencyStruct: CurrencyProtocol {
func currencyFunction() -> ReallyImportantCurrencyStruct.Currency {
return 2.0
}
}
let currency = ReallyImportantCurrencyStruct()
print(currency.currencyFunction())
protocol CurrencyProtocol {
typealias Currency = Double
}
struct ReallyImportantCurrencyStruct {
typealias Currency = Double
}
protocol CurrencyProtocol { }
extension CurrencyProtocol {
typealias Currency = Double
}
//: #Default associatedtype & overriding
//: Let's make an example of how can you use default associatedtype, to make clear what
//: it really does.
//: We will create generic protocol for Container, with associatedtype for ItemType.
//: Our ItemType will haave a default value of Int.
protocol Container {
associatedtype ItemType = Int