On Being a Less Horrible Monster
Reabstraction eats the world
This is an update on a previous post.
First, @jckarter pointed out that swift_func_object
is an artifact of the implementation. Swift is reabstracting the function to promote it to the most generic form (it's how a ()->Int
can be passed to something expecting ()->Void
). It turns out that Swift lifts functions to the maximally abstract form when taking a pointer or doing an unsafeBitCast
among other things.
In Swift 1.2 and earlier you'll also see a lot of reabstraction thunks in the call stack because self
is passed with a +1 ref count but Objective-C doesn't. In Swift 2.0 that is no longer true so I suspect a lot of that reabstraction will go away.
Second, Swift 2.0 adds the ability to directly work with C function pointers so this sort of awfulness isn't necessary anymore. The example from the release notes covers it pretty well:
var array = [3, 14, 15, 9, 2, 6, 5]
qsort(&array, array.count, sizeofValue(array[0])) { a, b in
//...
}
The CFunctionPointer
type no longer exists. Instead, functions/closures are just marked @convention(c)
to indicate they should use a C calling convention. Easy and painless.
This blog represents my own personal opinion and is not endorsed by my employer.