0

I saw this piece of code in the Swift 2.1 Programming Language Guide from Apple

func makeIncrementer() -> ((Int) -> Int) {
    func addOne(number: Int) -> Int {
        return 1 + number
    }
    return addOne
}
var increment = makeIncrementer()
increment(7)

While I do understand how the scope of the inner function is available outside it's execution block - thanks to this, I can't understand how passing 7 as argument to increment() is actually taken as an argument to the inner function, when increment() actually points to the outer function.

I also want to know when makeIncrementer() is actually executed? Does it when it is assigned to increment() or when increment() gets an argument passed?

Community
  • 1
  • 1
gtl_pm
  • 165
  • 1
  • 11

4 Answers4

1

makeIncrementer() is executed exactly when it looks like it's executed, on this line:

var increment = makeIncrementer()

Swift is by default an eager (rather than lazy) language. You can explore all of this by adding print statements at various points.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • I did. But I misunderstood what the playground output said. When I saw (Int->Int) I didn't pause to think that makeIncrementer is tied to a function as such and not to just a return value from that function. Believing in the latter led to the confusion. Thanks anyways! – gtl_pm Dec 19 '15 at 06:38
1

I can't understand how passing 7 as argument to increment() is actually taken as an argument to the inner function, when increment() actually points to the outer function.

The increment variable actually points to the inner function (addOne()), since that's what you returned from makeIncrementer(). So addOne() is called when you call increment(7).

I also want to know when makeIncrementer() is actually executed?

It is executed as soon as you call it:

var increment = makeIncrementer()
Greg Brown
  • 3,168
  • 1
  • 27
  • 37
  • Does it point to addOne() or does it point to the nameless method wrapping addOne()? I did understand the answer from @adpalumbo 's though. Thanks! – gtl_pm Dec 19 '15 at 06:39
1

Your first 6 lines define a function makeIncrementer that takes no arguments and returns a value. Like any other function would, it's invoked on line 7 with makeIncrementer().

The return value of that function happens to be another function of type (Int) -> (Int). That means that the returned function takes an argument of type Int and returns a value of type Int.

On line 7, the returned value (the (Int)->(Int) function) is assigned to increment.

So by line 8, the increment variable now represents a function that takes an Int and returns an Int. Like any other function, it's called using parenthesis and appropriate arguments: increment(7).

Note: Because the return value of increment(7) isn't assigned, the return value (which would be 8) is simply discarded.

adpalumbo
  • 3,031
  • 12
  • 12
0

makeIncrementer() is being executed when it's assigned to increment var. makeIncrementer() returns the function named addOne(number:).

Fantini
  • 2,067
  • 21
  • 32
  • Does it point to addOne() or does it point to the nameless method wrapping addOne()? I did understand the answer from @adpalumbo 's though. Thanks! – gtl_pm Dec 19 '15 at 06:40