On Darwin, Swift and Objective-C share enough of their basic object representation that (1) arbitrary Swift object references can be passed around in Objective-C as values of type id and (2) arbitrary Objective-C object references can be passed around in Swift as values of type AnyObject.[1] We also bridge Swift values to Objective-C object references and back again, and the efficiency (and "obviousness") of bridging relies on having a common representation, especially when bridging complex data structures. But we don't have to do some or all of that just to allow basic Objective-C interoperation; Swift also has the ability to work with "foreign" class models that stay distinct from its native types. So I think the first question is what we actually want at the language level for non-Darwin platforms.
I see three options for how this could work:
- exactly like it does on Darwin, and enabled in all Swift code;
- exactly like it does on Darwin, but you have to build Swift in some special mode to enable it; or
- treating Objective-C object references as a foreign class model. (
@objcclass hierarchies known to be defined in Swift might be able to straddle both worlds.)
I don't think there's anything technical about GNUStep that would prevent us from embracing a fully Darwin-like model of ObjC interoperation using the GNUStep runtime. It's mostly a question of if we actually want to. Tight interoperation has a bunch of real costs:
- Arbitrary
AnyObjects are not necessarily Swift objects, so reference counting, weak references, etc. are all slower than they are when we know we have a Swift object. We can dynamically optimize some of that runtime code for Swift objects, but doing so relies on assumptions about the ObjC runtime that might be reasonable in OS-distributed code (as on Darwin) but maybe not in a package ecosystem where the ObjC runtime is an independent peer. - Similarly, dynamic introspection and casting are slower because they have to check for an ObjC object.
- Swift's class objects have to function as ObjC class objects, so the static memory overhead of each class is substantially higher. On Darwin, this includes three extra words in the class object, but also a
class_ro_t, a metaclass object, aclass_ro_tfor the metaclass object, and whatever the ObjC runtime has to allocate dynamically to realize both classes (which itself is an extra dynamic execution overhead). I think most of these costs have parallels on other runtimes. - The need to handle bridged representations adds complexity and dynamic checks to a bunch of core data structures. This can be avoided by eagerly bridging, which is to say, creating a completely new data structure with the values of the old one, but of course that makes some kinds of interoperation both (1) much more expensive and (2) potentially lossy if e.g. a particular
NSArraysubclass has more information than is captured the superclass interface. - The ObjC runtime and Foundation both become core dependencies that must be linked and loaded in every process that uses Swift.
These costs are worth paying on Darwin because of Objective-C's importance to the platform API. Some of them are also minimized on Darwin; for example, there isn't much cost to linking the ObjC runtime and Foundation, because those libraries are loaded into most Darwin userspace processes anyway, and the OS optimizes for that. But I wouldn't want to force these costs on other platforms just in case something in the process uses Objective-C; among other things, it'd feel a little weird to privilege Objective-C that way. So, personally, I don't think I can support option #1.
Option #2 would basically be treating Swift+GNUStep as a platform variant; you'd need to build + distribute special binaries for it whenever you wanted to use it. This feels like it might be a configuration nightmare because you wouldn't be able to just use a standard Swift distribution, you'd need one specifically built with this support. But if GNUStep folks are willing to bear the primary burden of maintaining this, and accept that new things might be implemented without initial support for GNUStep,[2] I do think it's manageable.
Option #3 would make ObjC work substantially differently from how it does on Darwin, which might be its own little nightmare in the short term for both users and the language. For example, we'd need to introduce some sort of AnyObjCObject to import id as. But in the long term, if we can make sure that the support doesn't require linking the ObjC runtime unless ObjC is actually used, I think this is probably tenable.[3]
(Edited the first paragraph to clarify that there's a sort of spectrum of possible behavior, especially around bridging.)
which is also aligned with
AnyObjectgeneric requirements ↩︎I'll note that this is already true in clang, e.g.
directmethods are not currently supported on non-Darwin runtimes. ↩︎Especially if it doesn't require hard-coding details of the ObjC runtime into the Swift runtime, so that you could pick an arbitrary ObjC runtime dynamically. Darwin has a platform ObjC ABI, but there are alternatives to GNUStep on other platforms. ↩︎