I'm encountering an issue with my code and I'm trying to understand why the initial call to add
fails with a "Missing argument" error, while it runs successfully when passed to a different function where it is called without the argument labels.
I found a discussion on this topic at Why does Swift require parameter names if it also requires argument order?, but I'm having trouble grasping all the details and determining its relevance to my query.
func add(x: Int, y: Int) -> Int {
x + y
}
func wrapper(function: (Int, Int) -> Int) -> Int {
function(1, 2) // Why does this not produce a "Missing argument" error?
}
// "Missing argument" error
// print(add(1, 2))
// No Error - I think I understand why this works
print(add(x: 1, y: 2)) // Outputs: 3
// How does this work?
print(wrapper(function: add)) // Outputs: 3
print(wrapper(function: add)) // Outputs: 3
wrapper
is a function which receives 1 argument with this type:
(Int, Int) -> Int
The type of your add
function is
(Int, Int) -> Int
You can confirm this with Xcode:
Since the type of the argument of wrapper
is the same of add
, then this code compiles
wrapper(function: add)
can you explain a bit more on why the arguments are needed when its called outside and no longer needed when it's called inside a function?
Because:
So when you write
wrapper(function: add)
you are passing the add
function to wrapper
.
This is allowed because the types do match.
Then when you write
func wrapper(function: (Int, Int) -> Int) -> Int {
function(1, 2) // Why does this not produce a "Missing argument" error?
}
In the second line you are now invoking a closure
which does not have external parameter names.