

Why Swift closures are not Equatable
source link: https://www.jessesquires.com/blog/2021/04/05/why-swift-closures-are-not-equatable/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

05 Apr 2021
Updated: 06 Apr 2021
software-dev closures, compilers, swift
Despite the fact that closures (and functions) are reference types in Swift, they cannot be compared using ==
or ===
. But why?
I went down a rabbit hole today, because I thought I needed this (I did not). In other languages, like Objective-C, comparing function pointers is simple to do. In Swift, you will get an error. Attempting to compare two closures (or functions) with the same signature using ==
will produce the compiler error “Binary operator ‘==’ cannot be applied”. If you try to use ===
, the compiler will produce the error “Cannot check reference equality of functions”.
Searching for why, I eventually found this thread on the Apple Developer Forums. Ironically, it links to this StackOverflow thread, which links to this old Apple Dev Forums thread, which, of course, is now a broken link since no one at Apple could be bothered to do web development properly when the new forums launched last year.
Update: A reader has kindly pointed out that the broken link to Chris’s post was not lost last year, but rather in the previous transition from private to public forums, which happened some years earlier. I have lost count of how many times the Apple Dev Forums have undergone changes. This thread on the forums has details. It is, of course, answered by none other than the valuable, legendary Quinn.
Anyway. Apparently, somewhere in the ether of the old forums exists the following answer from Chris Lattner. (Thankfully, the person on StackOverflow directly quoted the forum thread instead of just posting a link.)
This is a feature we intentionally do not want to support. There are a variety of things that will cause pointer equality of functions (in the swift type system sense, which includes several kinds of closures) to fail or change depending on optimization. If “===” were defined on functions, the compiler would not be allowed to merge identical method bodies, share thunks, and perform certain capture optimizations in closures. Further, equality of this sort would be extremely surprising in some generics contexts, where you can get reabstraction thunks that adjust the actual signature of a function to the one the function type expects.
Not only can you not compare function pointers, if you could, you are asking for a world of trouble.
Now, you could dive into Swift’s “unsafe” APIs, create an UnsafePointer
, compare them, etc. But that is a very bad idea.™ Even if you end up writing something that passes a unit test, it will probably not work with optimizations turned on — or worse, it will only sometimes work. Or, it will break in the future.
Like I said, it turned out that I did not need to actually compare function pointers after all. There was a much better solution. You may think you need this. You (almost certainly) do not.
Recommend
-
13
David Cordero Swift: Avoid auto return in closures Published on 06 Dec 2014 In Swif...
-
9
Recipe for Calling Swift Closures from Asynchronous Rust Code The purpose of this recipe is to integrate asynchronous Rust with idiomatic Swift code that uses completion handler blocks. All reference counting s...
-
11
Swift enumerations and equatable Implementing equatable for enums with associated values 26 Jul 2015 ...
-
12
10 Jun 2018 software-dev
-
9
Reduce boilerplate code with an automatic synthesis of Equatable and Hashable conformance Table of ContentsEquatable and Hashable are two essential protocols in the Swift world. Toda...
-
10
« back — written by Brent on June 03, 2021 Why we need multi-line short closures in PHP ...
-
11
New issue Do not expand macros in equatable_if_let suggestion #7788
-
7
What is @escaping in Swift closures 19 Oct 2020 ⋅ 5 min read ⋅ Swift
-
15
Why can not I set a title for my UINavigationController in Swift? advertisements I have a UINavigationController
-
9
Why should we avoid using closures in Swift structs? Jan 10, 2020 • Rizwan •...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK