- Swift Functional Programming(Second Edition)
- Dr. Fatih Nayebi
- 116字
- 2021-07-02 23:54:27
Generics
Generic code enables us to write flexible and reusable functions and types that can work with any type, subject to requirements that we define. For instance, the following function that uses in-out parameters to swap two values can only be used with Int values:
func swapTwoIntegers(a: inout Int, b: inout Int) {
let tempA = a
a = b
b = tempA
}
To make this function work with any type, generics can be used, as shown in the following example:
func swapTwoValues<T>(a: inout T, b: inout T) {
let tempA = a
a = b
b = tempA
}
Generics will be covered in detail in Chapter 5, Generics and Associated Type Protocols.
推薦閱讀
- 企業(yè)數字化創(chuàng)新引擎:企業(yè)級PaaS平臺HZERO
- Test-Driven Development with Mockito
- 軟件成本度量國家標準實施指南:理論、方法與實踐
- 數據庫程序員面試筆試真題庫
- 一個64位操作系統(tǒng)的設計與實現
- SQL優(yōu)化最佳實踐:構建高效率Oracle數據庫的方法與技巧
- Oracle PL/SQL實例精解(原書第5版)
- MATLAB Graphics and Data Visualization Cookbook
- gnuplot Cookbook
- INSTANT Android Fragmentation Management How-to
- Spark分布式處理實戰(zhàn)
- 區(qū)塊鏈+:落地場景與應用實戰(zhàn)
- Filecoin原理與實現
- 算力經濟:從超級計算到云計算
- Unity for Architectural Visualization