Function with different signatures that can't coexist

This code is invalid:

Code Block
func name1() {
  func nest1() -> Int {
    print("nest1")
    return 3
  }
   
  func nest1() -> String {
    print("nest2")
    return "nest1"
  }
}

both nest1 function can't coexist.
But they have a different signature: one is returning an int and the other a string.

Aren't functions with different signatures, supposed to be able to coexist?
Answered by OOPer in 669197022

Aren't functions with different signatures, supposed to be able to coexist?

YES, when the functions are global:
Code Block
func nest1() -> Int {
print("nest1")
return 3
}
func nest1() -> String {
print("nest2")
return "nest1"
}


Or they are methods:
Code Block
class MyClass {
func nest1() -> Int {
print("nest1")
return 3
}
func nest1() -> String {
print("nest2")
return "nest1"
}
}


But, as for now, nested functions cannot be overloaded.
Your code may work in the future version of Swift:
Xcode 12.5 Beta 3 Release Notes

Updates in Xcode 12.5 Beta

Swift

Resolved Issues

・Function overloading now works in local contexts, making the following valid:
...
Please get the latest beta of Xcode 12.5 and try by yourself.
Accepted Answer

Aren't functions with different signatures, supposed to be able to coexist?

YES, when the functions are global:
Code Block
func nest1() -> Int {
print("nest1")
return 3
}
func nest1() -> String {
print("nest2")
return "nest1"
}


Or they are methods:
Code Block
class MyClass {
func nest1() -> Int {
print("nest1")
return 3
}
func nest1() -> String {
print("nest2")
return "nest1"
}
}


But, as for now, nested functions cannot be overloaded.
Your code may work in the future version of Swift:
Xcode 12.5 Beta 3 Release Notes

Updates in Xcode 12.5 Beta

Swift

Resolved Issues

・Function overloading now works in local contexts, making the following valid:
...
Please get the latest beta of Xcode 12.5 and try by yourself.
Normally, Swift func signature should include the return type.

OOPer gave the answer: that was a previous limit of Swift compiler.

However, this should not work, as compiler is unable to resolve ambiguity:
Code Block
func name1() {
func nest1() -> Int {
print("nest1")
return 3
}
func nest1() -> String {
print("nest2")
return "nest1"
}
print(nest1())
}

Function with different signatures that can't coexist
 
 
Q