We'll start with the playground from scratch and go through the code.
However you can also download a rich playground with more information for
your reference.
You may find the link in the instructor notes.
So what does it mean for a closure to be a first class citizen of the language?
It means you can use closures just like any other type, such as ants,
floats, strings.
Whatever.
You can assign them to variables or constants.
Put them inside arrays or dictionaries.
Return them, as the result of a function or closure.
And even receive them as parameters of another function or closure.
That last part seems a little bit weird so
let's start with the most obvious part.
First of all, we're going to assign a closure to the Bar or a constant.
Ok so here we are creating a closure using Syntax closure.
And we're assigning that closure to a constant.
So that's a little bit different, and And this closure to six.
When integer returns another integer and within the body of it,
it should be x by the way, it adds 42 to x.
Okay so it's a pretty simple closure.
And closures have types too.
Look right here in this result sidebar.
And you'll see that the type of this closure is Int arrow Int.
It means it takes one integer and returns an integer.
And we can call this closure just like we would call any function or
any other closure.
So, let's give it a try.
So, the bottom line is we can use the closure that we saved into the constant
F, in the normal way, using the regular syntax.
However, try to call it passing the string as a parameter.
This will cause an error and by now you should already know what's going on.
It's not the type that the closure accepts, so
it works exactly as functions, so far.
Let's get rid of this, and
now let's try something fancier, let's put a bunch of closures inside an array.
Let's see if it's possible.
So here we have an array called closures.
With just one closure, inside and the type,
is as you would expect, an array of int to int.
So this defines what kind of closure we can put inside.
Try this array.
As long as the closure takes one int and
returns an int, which would be able to put it in there.
Let's add a few more.
Okay, so here we have the final array with several closures in there.
All these closures have one thing in common.
They have the same type.
They take an int and return an int.
How does the compiler know that?
Because of the first one.
So this sets the type of closure that can go in there.
The second one over here has the full closure syntax with type information.
For the parameter and the return type, that's not really necessary,
because the compiler has that information already.
So the next one avoids the type information, and it works fine.
The other one, the next one.
Doesn't have the return, why?
Because when a closure just has one statement,
one line, the return is implicit.
So, the compliant knows it should return x times x.
And last but not least, we have this ultra simple version
where we don't even provide a name for the parameter because in Swift,
closures and functions can access their parameters or arguments.
By the position so dollar zero means position zero.
The first parameter times 42 and
return that.
Could we call those closures that are stored in the India rate.
Yes, definitely yes.
So let's give it a try.
So right here, we're iterating through the array of closures and
for each closure we call it passing 42.
And here you can see the results.
You can also see it this way.
Ok.
Now I've got an exercise for you.
Try to create an array with two closures.
One that takes two integers and returns the sum as another integer.
And the other closure should take two floats and return the sum as a float.
Would this compile?
Think why.