Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Closures Closures and Closure Expressions Using the Sorted Function

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

What's the logic behind the keyword "in" in closure expression syntax?

let numbers = [1,2,3,4,5]
let tripledNumbers = numbers.map({(i: Int) -> Int in return i * 3})
print(tripledNumbers)      //  [3, 6, 9, 12, 15]

Why is it the word "in"? It just seems like an arbitrary word for me right now.

3 Answers

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

I'll mostly cite from Apple Swift Docs - Closures here. Make sure to read through them, Apple docs are a perfect read in general after having completed a course, as they explain things in more depth.

So the chapter on closures has the following about in:

The start of the closure’s body is introduced by the in keyword. This keyword indicates that the definition of the closure’s parameters and return type has finished, and the body of the closure is about to begin

So a regular function declaration like this...

func backwards(s1: String, _ s2: String) -> Bool {
    return s1 > s2
}

...would look like this if it was written as a closure, where the in keyword indicates the begin of the closure's body.

{ 
    (s1: String, s2: String) -> Bool in
    return s1 > s2
}

Note that the declaration of parameters and return type for this inline closure is identical to the declaration from the backwards(::) function. In both cases, it is written as (s1: String, s2: String) -> Bool. However, for the inline closure expression, the parameters and return type are written inside the curly braces, not outside of them.

Hope that clarifies things a bit :)

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

I understand what the keyword "in" does. I just don't understand why they chose that word for this keyword. I'm trying to understand why things are the way the are, not just memorize arbitrary syntax.

Martin Wildfeuer
Martin Wildfeuer
Courses Plus Student 11,071 Points

Oops, sorry, I misunderstood that. In that case: good question :) I have to admit that I don't know the origin of the in keyword, but let me see if I can find something... Now I have to know it, too :)

Martin Wildfeuer
Martin Wildfeuer
Courses Plus Student 11,071 Points

Just to follow up our discussion: I asked around a bit and it seems there is no "official" or "historical" reason for the in keyword. Now that Swift is open source, I flicked through the repo, but I could not find any comments on this. Next step would be asking Lattner himself ;)

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

I think you might be onto something. In other words, we're saying I've defined some parameters and a return type, and now here is the main attraction wherein these things will be used.

Tommy Choe
Tommy Choe
38,156 Points

Hey Brendan, I'm not sure if I have it completely right but I'll try to answer your question to the best of my knowledge. The "in" keyword is there so that the compiler knows to separate the parameter (and possibly return type) from the statements. Without it, the compiler won't know when the statements will begin.

Keep in mind that the example you provided is an example of a simple closure expression with only one statement. So it looks like the compiler would know that the statements begin after the "return" keyword. However, with multiple statements, return could be associated with the last statement, not the first.

I hope I explained it in a way that makes sense. Let me know if it didn't.

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

Why the word "in" and not some other word? I've done a poor job of explaining my question. I'm not asking how the language works. I'm asking why they chose the word in to have this function as a keyword. The keywords "return" "func" "var" "if" "else" all seem fairly common sense, but "in" I don't understand why it is "in".

Tommy Choe
Tommy Choe
38,156 Points

Honestly I'm not really sure about that either. I've been wondering the same thing myself. My speculation is that it is meant to imply that the closure signature is used "in" the body. That's the best I've got.

Jasper Meurs
Jasper Meurs
3,170 Points

Did you find an answer to this question yet? I was asking myself the exact same question because I'm a person who needs to 'understand' why things are the way they are.