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
//: #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 | |
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
protocol CurrencyProtocol { } | |
extension CurrencyProtocol { | |
typealias Currency = Double | |
} |
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
struct ReallyImportantCurrencyStruct { | |
typealias Currency = Double | |
} |
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
protocol CurrencyProtocol { | |
typealias Currency = Double | |
} |
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
struct ReallyImportantCurrencyStruct: CurrencyProtocol { | |
func currencyFunction() -> ReallyImportantCurrencyStruct.Currency { | |
return 2.0 | |
} | |
} | |
let currency = ReallyImportantCurrencyStruct() | |
print(currency.currencyFunction()) |
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
let currencies = [CurrencyProtocol]() |
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
//: #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 |
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
struct LazyType { | |
lazy var soomethingLazy: String = { | |
return "Test lazy instance method" | |
}() | |
} |
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
struct LazyType { | |
// Normal usage | |
lazy var soomethingLazy: String = { | |
return "Test lazy instance method" | |
}() | |
// Lazy in statics | |
static var somethingLazy: String = { | |
return "Test lazy static method" |
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
// swift-tools-version:4.0 |
OlderNewer