I am rather new to Swift, but from what I have read about it and the little experience I have had with it so far I can tell that I like it a lot. I am a machine learning researcher and I think I would be interested in getting a project started to develop a Swift frontend for the popular ML library PyTorch (SwifTorch?), but it seems like this would be a lot easier if/when Swift C++ interoperability is relatively advanced (since the PyTorch internals are written mostly in C++).
I'm aware that it's possible to wrap C++ libraries in pure C and then work with them from Swift, but honestly I'm not sure I or others would be willing to put the time or energy in to do that. If it's possible to simply declare Swift structs and classes that contain C++ objects as properties, and call C++ methods on those objects, then developing the frontend would be a lot more straightforward.
So all that is to say, what is the state of C++ interoperability in Swift? Is it currently usable for a project like the one I'm describing?
Thanks,
Nora
PS: I am well aware that Swift for TensorFlow is a thing, but for better or worse the ML community has been tending to shift away from TensorFlow and toward PyTorch, so I think it would be quite valuable to have a version of PyTorch for Swift— potentially a lot more people could be brought over to using Swift that way.
You can also find out by downloading a development snapshot from Swift.org - Download Swift and trying to import PyTorch C++ headers (with swiftc -enable-cxx-interop) to see what works and what doesn't yet.
The short answer is: it's not yet ready. Depending on your library, you might be OK, but there's no guarantee, and I wouldn't be surprised if you run into some crashes, or linker errors. (In fact, I would be surprised if you don't.) Not to mention, many things may (and probably will) change in the next few months, so I wouldn't sink a bunch of work into a project just yet.
Here's a few important things that we do/don't support:
We have very rudimentary support for function templates.
Class templates should land in the next few days/weeks.
Fully specialized class templates are supported.
C++ constructors are supported.
C++ copy constructors/destructors should land in the next few days/weeks.
Move constructors are not supported (and won't be for some time, probably).
We don't yet support dependent types (those may be coming soon, though).
We can't yet import the C++ standard library.
Namespaces and other nested nominal types are mostly supported, some bug fixes for these should land in the next few days.
System C headers / Glibc support is finicky on Linux (I think C++ support will work best on macOS at this stage).
If you want to continue anyway... here are some suggestions:
Make sure to use a ToT build. Every few days we're submitting new fixes and improvements.
Write plain code; don't use templates; if possible create "C-like" bridge functions.
As Dan said, you can use -Xswiftc -enable-cxx-interop to enable C++, but this will implicitly disable C/Objective-C interop. So be careful that you're not using any of those libraries (such as Foundation).
Thank you for the information and your work on this. I will keep my eye on the C++ interoperability progress on the forums and on GitHub and probably wait a few months before trying to start SwifTorch in earnest.
Of course. When you do start the project, let me know. The more C++ interop projects we can monitor, the more bugs / areas for improvement we can find. And once the project starts, maybe we can try to maintain source compatibility with it (as an extra level of testing).
Hopefully we'll remedy this soon. I can't say what it will look like in the future, but I expect there to be a way to use Objective-C/C and C++ libraries all together.