- Hands-On Design Patterns with Swift
- Florent Vilmart Giordano Scalzo Sergio De Simone
- 262字
- 2021-07-02 14:45:01
Type aliases
Type aliases are a simple addition to the language; they let you reference simple or complex types by an alias. They support all declarations that you can imagine, from the simplest to the most complex.
The following block contains declarations for aliasing the following:
- A string class into a MyString
- A function declaration into a Block
- A block that takes any argument and returns any value
- A block that takes no argument and returns any value
Let's see the code block; they let you:
typealias MyString = String
typealias Block = () -> Void
typealias TypedBlock<T, U> = (T) -> U
typealias ReturningBlock<U> = () -> U
We could have also defined Block in the function of ReturningBlock:
typealias Block = ReturningBlock<()>
You can also use type aliases for protocol compositions and complex types, as follows:
- You can declare a type that conforms to a protocol and is of a particular class
- You can delete a type that conforms to multiple protocols
Let's see an example, as follows:
protocol SomeProtocol {}
protocol OtherProtocol {}
typealias ViewControllerProtocol = NSViewController & SomeProtocol
typealias BothProtocols = SomeProtocol & OtherProtocol
You will often find yourself using type aliases, in order to make your code more readable and more expressive. They are a powerful tool for hiding away some of the implementation complexity or verbosity when declaring long conformances. With type aliases, you can be encouraged to craft many protocols, each with a very small requirement list; then, you can compose all of those protocols when you need them, expressed as those types.
- Redis使用手冊
- 企業數字化創新引擎:企業級PaaS平臺HZERO
- 大數據技術基礎
- DB29forLinux,UNIX,Windows數據庫管理認證指南
- App+軟件+游戲+網站界面設計教程
- 新型數據庫系統:原理、架構與實踐
- Learning JavaScriptMVC
- R數據科學實戰:工具詳解與案例分析(鮮讀版)
- Live Longer with AI
- Starling Game Development Essentials
- SQL優化最佳實踐:構建高效率Oracle數據庫的方法與技巧
- 深入淺出 Hyperscan:高性能正則表達式算法原理與設計
- 大數據技術入門
- 數據庫技術及應用
- SAS金融數據挖掘與建模:系統方法與案例解析