3 ms·
I want to take the chance while Swift is on the front-page to talk about something mind-blowingly exciting (to me, at least) about it that people might not be a
by gavinray 5y ago
I want to take the chance while Swift is on the front-page to talk about something mind-blowingly exciting (to me, at least) about it that people might not be aware of:
>> Swift has support for C++ interop, without writing manual bindings! <<
Yes, you read that right. Swift can interop with and import C++ members/values, without needing to write a corresponding Swift type mapping. It's currently the only language I'm aware of that can do this (Nim doesn't count). I think it's because Swift is so integrated into LLVM/clang, that it can do a sort of mixed-compilation of Swift/CXX sources. Such that from a compiler's perspective which language they come from doesn't matter -- but I have no real clue.
It's very early, but it can already do some impressive things. The "cxx-interop" tag on the Swift Forums will show you what is basically a timeline of it's development and current status:
https://forums.swift.org/tag/cxx-interop
And here is the "C++ Interopability Manifesto", the outline they have for the feature in the language:
https://github.com/apple/swift/blob/main/docs/CppInteroperabilityManifesto.md
For a real-world example of using this, here's a test repo from the core developers:
https://github.com/plotfi/cxx-interop-test
https://github.com/plotfi/cxx-interop-test/blob/main/Sources/CXX/include/CXX.h
https://github.com/plotfi/cxx-interop-test/blob/main/Sources/Swift/main.swift
You see here direct import and use of "std::" namespace, including "std::vector" and "std::string_view", as well as use of structs and arrays:
import CXX // <-- This is CXX.h, local C++ file
import std.string_view
import std.vector
"42".withCString { cstring in
let view = std.__1.string_view(cstring)
assert(is_str_42(view))
}
var MS = MyStruct()
var MS1 = MS[0]
var v = V()
let arr = v.getVec()[0].swift_array()
print("std::vector: \(arr)")
Fucking mindblowing! It requires a ".modulemap" for the C++ code, but that can be auto-generated with most build tools AFAIK.
This is one of the most exciting things happening in tech for me. With Swift supporting Windows now, you have a great language for consuming C/C++ libraries and publishing native binaries or shared libs. Really looking forward to the future developments here.
I have never even written Swift, but I've been following the developments here closely. After Swift 5.5 features, looking like a really solid language if you want to do systems programming but can sacrifice some binary size and performance for a higher level language. Particularly the new Actors stuff for safe concurrency.
(Also, there's a chance Kotlin Native may get C++ support in the future, which would also be sweet)
- timtimmy 5y agoI share your enthusiasm for C++ interoperability. I'm a computer graphics researcher and I have written my share of C++. I'm using Swift on my current project and there are a wealth of C++ geometry and numerical libraries that I would love to integrate without having to jump through Objective-C or an automatic binding generator. Swift is a very pleasant language to work in compared to C++ (YMMV), so being free to pull in C++ libraries natively might make it attractive to a lot of folks.