We are continuing to investigate Swift/C++ interop (see manifesto and the forum discussion). To help guide our work, we’d like to hear how you would use this feature.
C++ is not an opinionated language. It accommodates a variety of coding styles and usage patterns. One can easily find projects that use C++ very differently.
If infinite engineering resources were available, Swift/C++ interoperability would accommodate most coding styles and use cases that C++ supports. In practice, we have limited resources, so we need to make prioritization decisions and resolve technical tradeoffs.
I would like to invite prospective users of Swift/C++ interop to discuss the use cases you have, your goals for using C++ interop, as well as the constraints that you operate under.
- How would you describe the C++ APIs that you are most interested in calling from Swift?
- Do they use C++ like “C with classes”, or do they use templates heavily?
- Do they use value types a lot, or do they have lots of polymorphic types with virtual functions?
- Do they make ownership explicit by using smart pointers, or do they require using new/delete?
- To what extent, if any, would you be able to change the C++ APIs to facilitate Swift/C++ interop?
- Note that our approach requires C++ headers to compile as a Clang module. Headers that don’t meet this criterion yet need to be “modularized”, and unfortunately we don’t see a way around this.
- We’re also thinking about ways in which optional changes or annotations to the C++ API can improve the quality of the imported Swift API. For example, annotating pointers with nullability qualifiers can turn the corresponding Swift types from optionals into non-optionals. Nevertheless, if you don’t annotate nullability, the imported APIs will be still usable, but they will have lots of implicitly unwrapped optionals.
- To what extent do you care about the performance overhead added by the interoperability layer? Would you prefer to see idiomatic Swift types in imported APIs at the cost of O(n) bridging conversions, or would you prefer high performance interoperability at the cost of seeing C++ types in imported APIs?
- How important is calling Swift from C++ for your use cases?
- Are you interested in just calling existing C++ code from Swift and vice versa, or are you also interested in migrating C++ code to Swift, using the Swift/C++ interop to facilitate an incremental rewrite?