- Hands-On Design Patterns with Swift
- Florent Vilmart Giordano Scalzo Sergio De Simone
- 193字
- 2021-07-02 14:44:59
Closures, functions, and currying
Closures are blocks of code that can be executed later, and functions are a special case of closures. Functions and closures can be passed around in your code, returned by other functions or closures. You can store a closure or a function in a variable, and execute them later:
let runMe = { () -> Int in
print(“run”)
return 0
}
runMe()
The preceding code is equivalent to the following:
func runMe() -> Int {
print(“run”)
return 0
}
runMe()
Closures and functions are almost always interchangeable, except when it comes to class or struct members:
class MyClass {
var running = false
lazy var runWithClosure: () -> Void = {
self.running = true
}
func runWithFunction() {
self.running = true
}
}
While both implementations are somewhat equivalent, we rarely want this function to be overridable at runtime. The closure can't reference self inside of it, unless marked lazy. Marking it lazy forces the implementation to be var, which, in turn, doesn't reflect what we want to express. In practice, we never declare instance methods as closures.
- MySQL高可用解決方案:從主從復制到InnoDB Cluster架構
- 達夢數據庫編程指南
- Effective Amazon Machine Learning
- 新型數據庫系統:原理、架構與實踐
- 數據結構與算法(C語言版)
- 數據驅動:從方法到實踐
- Sybase數據庫在UNIX、Windows上的實施和管理
- 企業級數據與AI項目成功之道
- SQL優化最佳實踐:構建高效率Oracle數據庫的方法與技巧
- “互聯網+”時代立體化計算機組
- INSTANT Android Fragmentation Management How-to
- 大數據分析:R基礎及應用
- SQL進階教程(第2版)
- Hands-On Java Deep Learning for Computer Vision
- 商業銀行數據庫管理實踐