且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

多个同名函数

更新时间:2023-02-23 21:58:10

如果两个函数的类型不同,或者可以通过它们的外部参数参数标签来区分,则允许定义两个具有相同名称的函数.函数的Type由括号中的参数Types,后跟->,后跟返回Type组成.请注意,参数标签不是函数类型的一部分.(但请参阅下面的更新.)

You are allowed to define two functions with the same name if they have different Types, or if they can be distinguished by their external parameter argument labels. The Type of a function is composed of the parameter Types in parentheses, followed by ->, followed by the return Type. Note that the argument labels are NOT a part of the function's Type. (But see UPDATE below.)

例如,以下函数具有相同的名称并且属于类型 (Int, Int) ->整数:

For example, the following functions both have the same name and are of Type (Int, Int) -> Int:

// This:
func add(a: Int, b: Int) -> Int {
    return a + b
}

// Is the same Type as this:
func add(x: Int, y: Int) -> Int {
    return x + y
}

这将产生编译时错误 - 将标签从 a:b: 更改为 x:y: 不会区分这两个函数.(但请参阅下面的更新.)

This will produce a compile-time error - changing the labels from a:b: to x:y: does not distinguish the two functions. (But see UPDATE below.)

以 Web 先生的函数为例:

Using Mr. Web's functions as examples:

// Function A: This function has the Type (UITableView, Int) -> Int
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { ... }

// Function B: This function has the Type (UITableView, NSIndexPath) -> UITableViewCell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { ... }

// Function C: This made up function will produce a compile-time error because
// it has the same name and Type as Function A, (UITableView, Int) -> Int:
func tableView(arg1: UITableView, arg2: Int) -> Int { ... }

上面的函数A和函数B并不冲突,因为它们是不同的类型.上面的函数A和函数C确实冲突,因为它们具有相同的类型.如果类型保持不变,更改参数标签并不能解决冲突. (请参阅下面的更新.)

Function A and Function B above do not conflict because they are of different Types. Function A and Function C above do conflict because they have the same Type. Changing the parameter labels does not resolve the conflict if the Types remain the same. (See UPDATE below.)

override 是一个完全不同的概念,我认为其他一些答案涵盖了它,所以我会跳过它.

override is a different concept altogether, and I think some of the other answers cover it, so I'll skip it.

更新:我上面写的一些内容是不正确的.确实,函数的参数标签不是它的类型定义的一部分,但它们可以用来区分具有相同类型的两个函数,只要该函数具有不同的外部标签,以便编译器可以分辨出哪个您在调用时尝试调用的函数.示例:

UPDATE: Some of what I wrote above is incorrect. It is true that a function's parameter labels are not a part of it's Type definition, but they can be used to distinguish two functions that have the same Type, so long as the function has external labels that are different such that the compiler can tell which function you are trying to call when you invoke it. Example:

func add(a: Int, to b: Int) -> Int {    // called with add(1, to: 3)
    println("This is in the first function defintion.")
    return a + b
}

func add(a: Int, and b: Int) -> Int {   // called with add(1, and: 3)
    println("This is in the second function definition")
    return a + b
}

let answer1 = add(1, to: 3)    // prints "This is in the first function definition"
let answer2 = add(1, and: 3)   // prints "This is in the second function definition"

因此,在函数定义中使用外部标签将允许编译具有相同名称和相同类型的函数.因此,似乎您可以编写多个具有相同名称的函数,只要编译器可以通过它们的类型或外部签名标签来区分它们.我认为内部标签不重要.(但如果我错了,我希望有人能纠正我.)

So, the use of external labels in a function definition will allow functions with the same name and of the same type to compile. So, it appears that you can write multiple functions with the same name so long as the compiler can distinguish them by their types or by their external signature labels. I don't think internal labels matter. (But I hope someone will correct me if I'm wrong.)