When will you use -> () in Swift?
Learn Swift and came across this example. What is the purpose of () -> on the first line and how could you use it in other simple examples?
func buildIncrementor() -> () -> Int {
var count = 0
func incrementor () -> Int {
++count
return count
}
return incrementor
}
var incrementor = buildIncrementor()
incrementor()
incrementor()
source to share
It reads like:
You create a function buildIncrementor
that takes no parameters and returns a function
-> ()
which returns an int:
-> Int
Although I am not writing this, when I code, I like to think as if I am reading it like:
func buildIncrementor() -> (() -> Int) {
var count = 0
func incrementor () -> Int {
++count
return count
}
return incrementor
}
Note the extra parenthesis. So it looks like I'm saying: buildIncrementor
returns another function. The function returned buildIncrementor
takes no parameters and returns Int
.
source to share
An empty tuple is the ()
same as Void
. You can read it as
func buildIncrementor() -> (Void -> Int)
a function buildIncrementor
that returns a type Void -> Int
, i.e. a closure / function, takes Void
(nothing) and returnsInt
BTW function can be simplified to
func buildIncrementor() -> () -> Int {
var count = 0
return { ++count }
}
source to share
The function buildIncrementor
accepts nothing and returns a function. And the return type of the function () -> Int
, which means it returns nothing Int
. Whenever it comes to an arrow, you are dealing with functions or closures. Things on the left are the input values ββand the values ββon the right output values.
source to share
buildIncrementor()
returns a function as a return type.
-
()
means - the function does not take any parameters -
-> Int
means it returns Int as its return type.
In general, it is something similar to:
func buildIncrementor() -> (Void) -> Int
{
}
or in another way:
func buildIncrementor() -> ((Void) -> Int)
{
}
incrementor() // Will Return 1
incrementor() // Will Return 2
You can read more about function return at Swift_Programming_Language / Functions
source to share
Your function is summarized as:
func incrementor (var value : Int = 0) -> () -> Int {
return { return value++ }
}
The function incrementor
takes one argument value
and returns () -> Int
, which itself is a function. There is no need to name the inner function incrementor
; you can just use anonymous closure. You can use this like
var inc_from_10 = incremenator (value: 10)
inc_from_10() // 10
inc_from_10() // 11
Functions that return functions are natural in a Swift-like language, where functions are "first class" (assigned to variables). Combined with common types and some complex shapes, they are formed.
// Return a function to test equality with an obj
func equate<T> (#pred: (T, T) -> Bool) (x:T) -> (T) -> Bool {
return { (y:T) in return pred (x, y) }
}
var equalToTen = equate (pred: ==) (x: 10) // curry syntax
// Return a function to complement a predicate
func complement<T> (#pred : (T) -> Bool) -> (T) -> Bool {
return { (x:T) in return !pred(x) }
}
var notEqualToTen = complement(equalToTen)
// Return a function conjoining two predicates
func conjoin<T> (#pred1: (T) -> Bool, #pred2: (T) -> Bool) -> (T) -> Bool {
return { (x:T) in pred1 (x) && pred2 (x) }
}
And now you will start using them, for example, in sequence functions like reduce
, map
and filter
.
source to share