# History: Why does closure syntax use the keyword \`in\`?

**URL:** <https://forums.swift.org/t/history-why-does-closure-syntax-use-the-keyword-in/21885>\
**Category:** Discussion\
**Created:** [March 21, 2019, 3:53pm UTC](https://forums.swift.org/t/history-why-does-closure-syntax-use-the-keyword-in/21885 "2019-03-21T15:53:09Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![fumoboy007](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/fumoboy007/32/898_2.png) [@fumoboy007](https://forums.swift.org/u/fumoboy007)\
**Post date:** [March 21, 2019, 3:53pm UTC](https://forums.swift.org/t/history-why-does-closure-syntax-use-the-keyword-in/21885/1 "2019-03-21T15:53:09Z")

</div>

I have a friend who is learning Swift and she doesn’t understand why closure syntax uses the keyword `in`. Honestly, I don’t understand either. Can someone link to or provide an authoritative answer regarding the choice of the keyword? Thanks!

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [March 21, 2019, 4:56pm UTC](https://forums.swift.org/t/history-why-does-closure-syntax-use-the-keyword-in/21885/2 "2019-03-21T16:56:33Z")

</div>

It's my fault, sorry. In the early days of Swift, we had a closure syntax that was very similar to traditional Javascript, `func (arg: Type, arg: Type) -> Return { ... }`. While this is nice and regular syntax, it is of course also very bulky and awkward if you're trying to support expressive functional APIs, such as `map`/`filter` on collections, or if you want libraries to be able to provide closure-based APIs that feel like extensions of the language. Our earliest adopters at Apple complained about this, and mandated that we support Ruby-style trailing closure syntax. This is tricky to fit into a C-style syntax like Swift's, and we tried many different iterations, including literally Ruby's `{|args| }` syntax, but many of them suffered from ambiguities or simply distaste and revolt from our early adopters. We wanted something that still looked like other parts of the language, but which could be parsed unambiguously and could span the breadth of use cases from a fully explicit function signature to extremely compact. We had already taken `in` as a keyword, we couldn't use `->` like Java does because it's already used to denote the return type, and we were concerned that using `=>` like C# would be too visually confusing. `in` made `xs.map { x in f(x) }` look vaguely like `for x in xs { f(x) }`, and people hated it less than the alternatives.

---

<div class="post-metadata">

**Author:** ![fumoboy007](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/fumoboy007/32/898_2.png) [@fumoboy007](https://forums.swift.org/u/fumoboy007)\
**Post date:** [March 21, 2019, 5:27pm UTC](https://forums.swift.org/t/history-why-does-closure-syntax-use-the-keyword-in/21885/3 "2019-03-21T17:27:41Z")

</div>

> [@Joe\_Groff](#):
>
> It's my fault, sorry.

No need to apologize; you helped make the greatest programming language ever made. 😊

I didn’t expect such a fast and detailed response. Thank you!

---

<div class="post-metadata">

**Author:** ![fswarbrick](https://avatars.discourse-cdn.com/v4/letter/f/54ee81/32.png) [@fswarbrick](https://forums.swift.org/u/fswarbrick)\
**Post date:** [March 21, 2019, 8:38pm UTC](https://forums.swift.org/t/history-why-does-closure-syntax-use-the-keyword-in/21885/4 "2019-03-21T20:38:48Z")

</div>

@fumoboy007, thanks for the question, as it's bugged me since I first saw it.  
And thanks @Joe_Groff for the answer; even if I still find the syntax awkward. (I love trailing closures. I just dislike the "in" syntax.)

---

<div class="post-metadata">

**Author:** ![svanimpe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/svanimpe/32/38963_2.png) [@svanimpe](https://forums.swift.org/u/svanimpe)\
**Post date:** [March 21, 2019, 8:41pm UTC](https://forums.swift.org/t/history-why-does-closure-syntax-use-the-keyword-in/21885/5 "2019-03-21T20:41:28Z")

</div>

@fumoboy007 When teaching, I explain it as actors (parameters) starring **in** a movie (block of code). That seems to help students learn/remember the syntax.

---

<div class="post-metadata">

**Author:** ![Tino](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tino/32/1073_2.png) [@Tino](https://forums.swift.org/u/Tino)\
**Post date:** [March 21, 2019, 8:46pm UTC](https://forums.swift.org/t/history-why-does-closure-syntax-use-the-keyword-in/21885/6 "2019-03-21T20:46:40Z")

</div>

I'd love to see a series of short blogposts with background information like this...

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [March 21, 2019, 8:58pm UTC](https://forums.swift.org/t/history-why-does-closure-syntax-use-the-keyword-in/21885/7 "2019-03-21T20:58:33Z")

</div>

A good amount of our early syntax wars is still preserved in the compiler's git history, at least.

---

<div class="post-metadata">

**Author:** ![anandabits](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anandabits/32/355_2.png) [@anandabits](https://forums.swift.org/u/anandabits)\
**Post date:** [March 21, 2019, 9:04pm UTC](https://forums.swift.org/t/history-why-does-closure-syntax-use-the-keyword-in/21885/8 "2019-03-21T21:04:17Z")

</div>

There's also an analogy with ML's `let x = 42 in ...`. I was already familiar with that so I guess it has always made sense to me for that reason.

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [March 21, 2019, 9:04pm UTC](https://forums.swift.org/t/history-why-does-closure-syntax-use-the-keyword-in/21885/9 "2019-03-21T21:04:48Z")

</div>

True, with capture lists, closures are also secret let-expressions…

---

<div class="post-metadata">

**Author:** ![zpapp](https://avatars.discourse-cdn.com/v4/letter/z/4491bb/32.png) [@zpapp](https://forums.swift.org/u/zpapp)\
**Post date:** [March 24, 2019, 8:08pm UTC](https://forums.swift.org/t/history-why-does-closure-syntax-use-the-keyword-in/21885/10 "2019-03-24T20:08:04Z")

</div>

I like "starring in" better than the official explanation! 🙂

---

<div class="post-metadata">

**Author:** ![Chris\_Lattner3](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chris_lattner3/32/348_2.png) [@Chris\_Lattner3](https://forums.swift.org/u/Chris_Lattner3)\
**Post date:** [March 27, 2019, 3:25am UTC](https://forums.swift.org/t/history-why-does-closure-syntax-use-the-keyword-in/21885/11 "2019-03-27T03:25:31Z")

</div>

The entry for this in [the changelog](https://github.com/apple/swift/blob/master/CHANGELOG.md) is at the very bottom of the file. 🙂

---

<div class="post-metadata">

**Author:** ![ZevEisenberg](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/zeveisenberg/32/11574_2.png) [@ZevEisenberg](https://forums.swift.org/u/ZevEisenberg)\
**Post date:** [April 6, 2019, 3:49pm UTC](https://forums.swift.org/t/history-why-does-closure-syntax-use-the-keyword-in/21885/12 "2019-04-06T15:49:23Z")

</div>

I love the “starring in” approach! I’ve previously explained it as the [_dramatis personæ_](https://en.wikipedia.org/wiki/Dramatis_person%C3%A6?wprov=sfti1) of the function, but I think the movie stars analogy works even better.

---

<div class="post-metadata">

**Author:** ![algebra8](https://avatars.discourse-cdn.com/v4/letter/a/d07c76/32.png) [@algebra8](https://forums.swift.org/u/algebra8)\
**Post date:** [January 25, 2021, 2:08am UTC](https://forums.swift.org/t/history-why-does-closure-syntax-use-the-keyword-in/21885/13 "2021-01-25T02:08:59Z")

</div>

This is a great question and a great answer. For another perspective: I'm very new to Swift and come from Python. I found `in` for closures confusing because I am used to it being heavily used for testing membership, mainly with iterators.
