Bumble Apr 16, 2018

Swift 4.1: Introduction of CompactMap

Article Summary

Muhammad Shuaib Khan from Bumble Tech explains why Swift 4.1 killed off a popular method. The culprit? A naming confusion that was breaking code in subtle, dangerous ways.

Swift's flatMap had three different overloads doing very different things, and one was causing silent bugs when developers thought they were using map. When String became a Collection in Swift 4.0, innocent-looking code started returning completely wrong types without any compiler errors.

Key Takeaways

Critical Insight

Swift 4.1 introduced compactMap to replace flatMap's nil-filtering behavior, eliminating a confusing overload that caused type mismatches and silent logic bugs.

The article includes code examples showing exactly how the String Collection conformance turned correct-looking code into a type nightmare.

Recent from Bumble

Related Articles