[Accepted] A vision for using Swift from C++

Hello, Swift community.

I'm pleased to announced that the Language Workgroup has accepted its first vision document, entitled Using Swift from C++:

This vision document presents a high level overview of the "reverse" (i.e. Swift-to-C++) half of the C++ interoperability Swift language feature. It highlights the key principles and goals that determine how Swift APIs are exposed to C++ users. It also outlines the evolution process for this feature. This document does not present the final design for how Swift APIs get mapped to C++ language constructs. The final design of this feature will evolve as this feature goes through the Swift evolution process. This document does not cover the "forward" (i.e. using C++ APIs from Swift) aspect of the C++ interoperability, as it’s covered by a sibling document.

This vision document was written by Alex Lorenz of the C++ interoperability workgroup with the cooperation and input of the rest of the workgroup, as well as some feedback from the Language Workgroup.

As discussed in my general post about vision documents, the Language Workgroup's acceptance of this document is a strong endorsement of the goals laid out in the vision, a general endorsement of the basic approach, but only a weak endorsement of any concrete proposals. All proposals in the vision will have to undergo ordinary evolution review, which may result in rejection or major revision.

Please feel free to discuss this vision in this thread.

John McCall
Language Workgroup

44 Likes

This sounds good. Uncontroversial, I think.

Since one of the goals of this work is to enable C++ developers to integrate Swift code, I hope we also make a point of reaching out to some C++ developer communities when concrete proposals are ready, so we can invite them to also participate in the process. The users available to review proposals here on the official Swift forums are likely to be more recently familiar with Swift than C++.

It can also serve to raise awareness that this work is happening. Some developers may be curious to see how their APIs get imported by Swift, and hopefully they'll be impressed by what they see.

17 Likes

Great.

…Maybe much too early to ask: Is there any rough idea about the timeframe when a first version could be ready?

Depends what you mean by first version, but we’ve been using cpp interop actively using nightly tool chains in a fairly complex c++ project already for a while. Worth trying out what already is there today :slight_smile:

2 Likes

I have read the vision document and a little of implementation.
It is a smart design to have C++ compiler support Swift's calling convention so that it can export to C++ without any opt-in work like @objc.
Attributes like swiftcall and swift_context are ultimately handled by LLVM, so the clang changes seem relatively lightweight.

But how will you support GCC, Visual Studio, and the many other C++ compilers?
Are there any plans to do?

2 Likes

Since GCC C++ and LLVM C++ already don't mix for advanced features, I don't see this as an issue. Swift is LLVM-based, so it makes sense just to support LLVM C++. Visual Studio supports LLVM C++.

1 Like

I mean calling Swift from C++ as described in this post, from my understanding this is not yet realized?

Yes, you can already call swift form c++. Many situations already work, though still there’s missing things here and there, mostly around things that touch templates or move only types or other advanced c++. But basic things work just fine—basic reference or value types etc.

4 Likes

OK, so this proposal builds on this, or is the proposal “a new way”?

And OK, I see the documentation for what is already there.

This is a vision document laying out the work that makes that happen. Significant amounts of it have already been prototyped. That work will be broken down into proposals and reviewed over the coming months.

7 Likes

Huge congratulations to Alex Lorenz and others working on this!

7 Likes