官术网_书友最值得收藏!

First-class functions

In the Function types section of this chapter, we saw that we can define function types and store and pass functions around. In practice, this means that Swift treats functions as values. To explain this, we will need to examine a couple of examples:

let name: String = "Grace" 

In this code example, we create a constant of the String type name and store a value ("Grace") into it.

When we define a function, we need to specify the type of parameter, as follows:

func sayHello(name: String) { 
print("Hello, \(name)")
}

In this example, our name parameter is of the String type. This parameter could be any other value type or reference type. Simply, it could be Int, Double, Dictionary, Array, Set, or it could be an object type such as an instance of class, struct, or enum.

Now, let's call this function:

sayHello(name: "Your name") // or 
sayHello(name: name)

Here, we pass a value for this parameter. In other words, we pass one of the previously mentioned types with their respective values.

Swift treats functions like the other aforementioned types so we can store a function in a variable as we were able to do with other types:

var sayHelloFunc = sayHello 

In this example, we saved the sayHello function in a variable that can be used later and passed around as a value.

In pure OOP, we do not have functions; instead, we have methods. In other words, functions can only reside in objects and then they are called methods. In OOP, classes are first-class citizens and methods are not. Methods are not solely reachable and cannot be stored or passed around. In OOP, methods access the object's data that they are defined in.

In FP, functions are first-class citizens. Just like other types, they can be stored and passed around. In contrast to OOP, that method can only access their parent object's data and change it; in FP, they can be stored and passed to other objects.

This notion enables us to compose our applications with functions as they are just another type that can be used. We will talk about this in more detail; for now, it is important to understand why we define functions as first-class citizens in Swift.

主站蜘蛛池模板: 类乌齐县| 深泽县| 康平县| 永新县| 桐梓县| 双流县| 铅山县| 丰城市| 天峻县| 高雄市| 绥德县| 谢通门县| 通江县| 迭部县| 益阳市| 彰武县| 大化| 曲水县| 唐海县| 宁南县| 无锡市| 安西县| 全椒县| 奎屯市| 章丘市| 常宁市| 武鸣县| 吐鲁番市| 嵊州市| 阳谷县| 衡南县| 巴中市| 沐川县| 铜川市| 仁寿县| 织金县| 嵩明县| 桑植县| 宝丰县| 宜黄县| 武夷山市|