What exactly distinguishes the two functions in Swift?

Interestingly, it distinguishes two "similar functions" from each other and can be unambiguously called

This is what I learned from self-study.

Uniqueness comes from: Function name + Argument and argument name + return type. Their combination must be unique in order to make a unique function (see Example below)

  • Nmu1and Num2will cause ambiguity, because the return type (does not have a so-called return name, the function name already acts like this role) is not passed when the function is called

  • Although the function Num3has a different parameter name, it will not unambiguously separate it from the function in Num1and Num2. Since the argument name will not be passed when the function is called, only another argument name will not make the function unique; And the function from Num4is different from all the above functions, because its signature Function name + Order argument and argument name + return type are unique among all the previous three functions.

  • Num5and Num6functions differ from each other because they have different orders of arguments when they were defined

    //Num1
    func foo(guy name: String) -> String {
         return "Hi \(name)"
    }
    
    //Num2
    func foo(guy name: String) {
        print("Hi \(name)")
    }
    
    //Num3
    func foo(guy called: String) -> String {
        return "Hi \(called)"
    }
    
    //Num4
    func foo(dude name: String) -> String {
        return "What up \(name)"
    }
    
    //Num5
    func foo(man name: String, from place: String) {
       print("Hi! I'm \(name) I come from \(place)")
    }
    
    //Num6
    func foo(from place: String, man name: String) {
       print("Hi! I'm \(name) I come from \(place)")
    }
    

Question : I could have missed, or perhaps even misunderstood some parts. It would be very nice that you can correct me and add those parts that I missed.

thank

[] . , . !

, . ; . . .

, , . .

, , .

func foo(dude name: String) -> Int {
    print("Hi \(name)!")
    return 1
}

func foo(man name: String) -> Int {
    print("Hi \(name)!")
    return 1
}

//foo(dude: String) & foo(man: String) is identical when calling 

+4
1

Obj-C- , Num1 Num2 , , NSObject; @nonobjc, , ​​ Swift :

//Num1
@nonobjc func foo(guy name: String) -> String {
     return "Hi \(name)"
}

//Num2
@nonobjc func foo(guy name: String) {
    print("Hi \(name)")
}

, , .

+3

Source: https://habr.com/ru/post/1654801/


All Articles