Why Swift closures are not Equatable
05 April 2021
Despite the fact that closures (and functions) are reference types in Swift, they cannot be compared using ==
or ===
. But why?
turing complete with a stack of 0xdeadbeef
Despite the fact that closures (and functions) are reference types in Swift, they cannot be compared using ==
or ===
. But why?
In JavaScript, this pattern is called an Immediately Invoked Function Expression (IIFE) or a Self-Executing Anonymous Function. Swift doesn’t have an “official” name for this, but IIFE works as well as “immediately executed anonymous closure” or “self-executing anonymous closure”. (Thanks to folks on Twitter for helping with this.)
In a recent episode of the podcast, JP and I discussed the implicit escaping of closures in Swift. As Swift has matured and evolved, the default behavior of closure parameters in functions has changed. Prior to Swift 3, closures parameters were escaping by default. After SE-103, the default was changed to non-escaping.