- 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.
- 數據要素安全流通
- 數據可視化:從小白到數據工程師的成長之路
- 云數據中心基礎
- 正則表達式必知必會
- Architects of Intelligence
- 工業大數據分析算法實戰
- R數據科學實戰:工具詳解與案例分析(鮮讀版)
- INSTANT Cytoscape Complex Network Analysis How-to
- 智能數據時代:企業大數據戰略與實戰
- “互聯網+”時代立體化計算機組
- LabVIEW 完全自學手冊
- Oracle PL/SQL實例精解(原書第5版)
- 云數據中心網絡與SDN:技術架構與實現
- Scratch 2.0 Game Development HOTSHOT
- 數據庫原理及應用:SQL Server 2016