I saw some code on another question that seems to create an anonymous function (closure expression) with some unusual syntax:
let plus: (Int, Int) -> Int = (+)
I understand the left side—that it's declaring a constant of type (Int, Int) -> Int
(a function that takes two Integers and returns an Integer). But what is (+)
? How can it declare a function without curly brackets, and how does it refer to the two arguments when there are no argument labels of any kind?
The function takes two arguments, adds them together, and returns the result. If I replace the +
operator with a different one (say a *
), the operation changes. So is it some kind of shorthand for {$0 + $1}
? If so, what is the logic behind this shorthand?
+
is aninfix
operator and a function name in Swift. There are many such functions defined on many types (it is overloaded).You can define
+
for your own custom type. For example:This works:
because the signature of the
+
function has been fully specified, so Swift is able to identify the correct+
function.And if we want to assign our new
+
function toplus
:It's really no different than this:
+
is also a unary operator in Swift.For example, you can say:
So just trying to say:
confuses the compiler because it is treating the
+
as a unary prefix operator and it is expecting the+
to be followed by something else such as5
. By surrounding it with parentheses, the Swift compiler then stops trying to parse+
as a unary operator and treats is just as its function name. Even if+
weren't a unary prefix operator, Swift would still be expecting values on either side of the+
, so the parentheses tell Swift that you aren't providing any inputs to the function, but just want the function itself.You can refer to the
+
function without the parentheses in situations where it isn't ambiguous. For example:Actually, this is no shorthand.
plus
is a variable of type(Int, Int) -> Int
. You can assign it any object that is of this type (or any of its subtypes). A literal lambda closure is certainly of this type, but actually a named function or method would also do. And that is exactly what is happening here.It is assigning the operator method object named
+
to the variable.This is mentioned sort-of implicitly in the Closures chapter of the language guide:
So, what the code is doing is assigning the Operator Method
+
to the variableplus
.+
is simply the name of the function assigned to the variable. No magic shorthand involved.Would you be surprised to see this?
(+)
by itself is an operator method. You can declare your own operator like this:Usage will be the same: