How Implicit Return Types Removed that Annoying Swift Inconsistency

It took SO LONG to fix this

Steven Curtis
3 min readMar 18

--

Photo by Ryan Stone on Unsplash

For a long time we haven’t been living in a fair environment. It’s true: With a closure you can omit the return keyword, but you could never do the same in a function. THAT GOT FIXED!!

Difficulty: Beginner | Easy | Normal | Challenging

This article has been developed using Xcode 11.4.1, and Swift 5.2.2

The example

Most Swift ninjas (those with 6 months+ of experience, that is) know writing short and elegant code is extremely important, and helps us write easily understandable code that is a pleasure to maintain.

Closures have helped us do this for some time, I mean look at the following examples (which are all equivalent!, presuming we have let arr = [1,2,3,4,5]):

We FINALLY have parity for functions, and the following can be called through helloToTheWorld() — isn’t that awesome?!?

Click for Gist

REMEMBER THOUGH

It only works with one liners. Whack a print statement into the above to get it to be two lines and you’ll have to add return.

Still it’s awesome!

Why it matters

When we talk about consistency in your code, it makes sense that we have that same consistency in programming languages. By bringing functions into line with closures Swift has done just that — and for this reason we should rejoice and thank the Swift team, or something.

Conclusion

What a tip. At least you won’t have a heart attack when you see that there is no return in a function. So that’s something!

If you’ve any questions, comments or suggestions please hit me up on Twitter

Thanks for reading!

Subscribing to Medium using this link shares some revenue with me. Or click the rather large banner below! Any revenue encourages me to create more articles. I know I have a large amount of articles, but some of these earn less than $1 dollar and without some support I find it difficult to justify time away from my family to create these tutorials (so any help is much appreciated).

You might alternatively like to give me a hand by buying me a coffee https://www.buymeacoffee.com/stevenpcuri.

If you’ve any questions, comments or suggestions please hit me up on Twitter

--

--