SE-0255: Implicit Returns from Single-Expression Functions

I think closures are a different story.

collection.filter { $0.age >= 18 }.map { $0.name }

With closures, you tend to think only about the expression. In the example above, you're filtering based on a condition and mapping to a value. You're not really thinking about declaring a function that returns a value; that's just an implementation detail.

My experiences with closures are that:

  • students with prior programming experience tend to struggle with the syntax in general because they've only used arrow syntax so far (e.g. in Java or C#).
  • students learning Swift as their first language have less issues with the syntax because they have no preconceptions as to what closures should look like. They tend to struggle mostly with trailing closures. I've seen a lot of these students avoid trailing closures at first and only adopt them when they have a better understand of closures in general.
2 Likes