News for hackers. Stuff that matters.
Re:Swift and C++ interoperability workgroup announcement by sirwhinesalot on Tuesday February 01, 2022 @12:45PM (#30166229)
I wish them the best, but getting two of the most complicated languages available today with widely different semantics talking to each other is going to be a tall order.

I wish they'd start by standardizing something equivalent to "extern C" in Swift, so other languages can consume (wrapped) Swift libraries without having to implement support for the rather complicated Swift ABI.

Re:Swift and C++ interoperability workgroup announcement by favorited on Tuesday February 01, 2022 @04:03PM (#30169216)
> I wish they'd start by standardizing something equivalent to "extern C" in Swift

There is the unofficial (but very relied upon) @_cdecl, if you're OK with needing to update your code if/when that eventually gets standardized. That attribute will take care of the calling convention difference for you.

https://github.com/apple/swift/blob/main/docs/ReferenceGuide...

Re:Swift and C++ interoperability workgroup announcement by sirwhinesalot on Tuesday February 01, 2022 @06:33PM (#30171299)
I'm aware, but it's still unofficial and I've been burned by depending on such things before. Hopefully it'll become official soon.
Re:Swift and C++ interoperability workgroup announcement by estebank on Tuesday February 01, 2022 @01:12PM (#30166635)
I think there's space for a high-level ABI that specifies little more than C + VTables + callback register (and optionally ownership information, even if ignored by the caller or host) that multiple languages could decide to use for interop. COM already exists and its a shame that it simultaneously does too much and too little to aid in safely exposing APIs, but I would love to see its ideas pushed further.
Re:Swift and C++ interoperability workgroup announcement by sirwhinesalot on Tuesday February 01, 2022 @01:28PM (#30166902)
I've discussed a similar concern in another hackernews post, though there I suggested improvements to the C language itself since it's the one standard everyone is somewhat forced to follow.

The problem with such an ABI is getting people to use it, there's no single entity with enough power to enforce it.

I've thought of tackling the problem in another direction, making a simple C-like language (but slightly higher level) which uses the C ABI but with proper support for slices/strings/interfaces/errors/etc. and automatically produces wrapper code for mainstream languages (C++, Java, C#, Swift).

The wrappers wouldn't be idiomatic code in the host language, but they'd be directly usable, whereas right now wrapping a "C" library (even when produced by a higher level language) requires recovering all of the semantic information in an additional, library-dependent high-level wrapper.

For example, some SMT solvers expose quite nice C APIs, but to use them in say... Python, you'd need to use ctypes and a lot of low level code, which is why they go out of their way to expose wrapper APIs.

Some solvers like Yices2 have really low level Python wrappers, which mostly take care of the memory management, unpacking lists into a pointer and a length, and string conversions. There's no reason that couldn't be automated with a slightly higher-level than C language.

Re:Swift and C++ interoperability workgroup announcement by estebank on Tuesday February 01, 2022 @01:46PM (#30167170)
Focusing on the "you have now N + 1 standards" problem, I think following the Language Server strategy is the way to go: you don't force people to support it, you focus on specifying an API that support a handful of representative bridges (let's say Python <-> Swift <-> Java <-> Haskell), with a builtin method to evolve and then let people realize that supporting this new ABI immediately brings support for every other language that supports it in an idiomatic-ish way.
Re:Swift and C++ interoperability workgroup announcement by sirwhinesalot on Tuesday February 01, 2022 @01:57PM (#30167358)
Yes, the wrapper generators would not be built into the compiler, they're just little pieces of software that generate the relevant code (anyone could develop one in their own language in a couple of days).

The language exists merely as a way of directly interfacing with the ABI, a way to get the libraries out the door so interest in the ABI exists (same as how VSCode made LSP popular).

The problem with doing Python <-> Swift is that you cannot (and should not) try to expose the entirety of Python and Swift to either side. So you need an "extern ABI" solution to expose your Swift code, except this is not built in to the Swift compiler so now you are in trouble.

If you go the external route you end up with SWIG, which is something you only use when you have no other option (CVC4 moved away from it for example).

Having a language that directly supports the ABI sidesteps this issue. All code written in the language works with the ABI directly so you get the wrappers for free without thinking about how to implement them. A lot of libraries are written in C directly for this very reason!

Once it becomes popular enough, languages would implement "extern ABI" directly, and can now talk to each other over it without the middleman. Same as how a compiler with LSP support can be used in editors that are not VSCode.

Re:Swift and C++ interoperability workgroup announcement by pjmlp on Wednesday February 02, 2022 @02:16AM (#30174722)
> The problem with such an ABI is getting people to use it, there's no single entity with enough power to enforce it.

There is on Apple (XPC), Microsoft (COM/UWP) and Google (JNI, Android IPC, Fuchsia IDL), IBM (TIMI / Language Environments).

The problem are UNIX FOSS clones that lack such overlords.

Re:Swift and C++ interoperability workgroup announcement by sirwhinesalot on Wednesday February 02, 2022 @05:00AM (#30175670)
Those OS solutions aren't cross platform (obviously) so they're only used reluctantly for the smallest amount of code necessary to interface with the platform. Microsoft went out of their way to support WinRT in Rust because sure as heck nobody else was going to.

That said, if IPC solutions count (which to me they don't), then the Linux standard would be D-Bus.

Re:Swift and C++ interoperability workgroup announcement by pjmlp on Wednesday February 02, 2022 @05:07AM (#30175717)
For me they count, because it is a way to improve overall application security.

icrosoft went out of their way to support WinRT in Rust, because they want to use Rust themselves, it wasn't out of love.

Same applies with Google's support for Android IPC and Fuchsia IDL for Rust.

Naturally the platforms with headless management like UNIX FOSS clones will never adopt those solutions at scale, hence why nobody cares about D-BUS.

Re:Swift and C++ interoperability workgroup announcement by sirwhinesalot on Wednesday February 02, 2022 @05:23AM (#30175812)
Oh don't get me wrong, for platform APIs IPC is probably the way to go. Microkernels are built entirely around IPC after all.

But I don't want to have to start a separate process and serialize data across a socket to call functions in every library that's packaged with my software, it's too expensive.

Even if the same IPC approach is used in-process and we accept the serialization costs (which is fine), why would library X use this approach and not another one? Specially if it needs to also run on macOS? Different IPC solutions per platform? Nah.

My point about Microsoft is that, had they not supported Rust themselves, it's likely nobody would have bothered with Rust projection for WinRT. That shows that even the OS devs lack the "strength" to enforce these things.

C is the glue everyone uses and will continue to be so for the foreseeable future.

Re:Swift and C++ interoperability workgroup announcement by pjmlp on Wednesday February 02, 2022 @08:15AM (#30177029)
> My point about Microsoft is that, had they not supported Rust themselves, it's likely nobody would have bothered with Rust projection for WinRT. That shows that even the OS devs lack the "strength" to enforce these things.

Nope, it shows that Rust would be irrelevant for most Windows shops, and Microsoft wants to use Rust instead of C++ in certain workloads like they are already shipping on Azure IoT.

Same thing with Android and Fuchsia, good luck using your C glue there.

Re:Swift and C++ interoperability workgroup announcement by sirwhinesalot on Wednesday February 02, 2022 @08:39AM (#30177314)
Most Windows shops could not care less about WinRT. Nobody is going to develop libraries that expose a WinRT interface besides Microsoft, nobody is going to use it unless they must, just as almost nobody exposes COM APIs unless they need to run within Office or something. WinRT is so unpopular that all language support was developed by Microsoft themselves.

I think we're talking over each other. I'm not following your point. Do you believe WinRT is going to become the new standard for cross-language interoperability, even on Windows?

Libraries are either OS libraries, in which case something wraps the "platform ABI" and hides the interface (see SDL on Android for example), language specific (.NET libs are only for .NET languages), have custom tailored language A <-> language B binding generators (e.g. Shiboken to expose C++ Qt to Python), or expose a C API.

Even tools like Xamarin to expose Android APIs to .NET rely heavily on intermediate C glue because that's the only alternative (besides IPC, which is not desirable) to avoid running on the JVM.

Re:Swift and C++ interoperability workgroup announcement by pjmlp on Wednesday February 02, 2022 @10:47AM (#30179181)
You just don't get the point, WinRT and COM are exactly the same thing.

COM with .NET metadata files + IInspectable in addition to IUnknown => WinRT

Since Windows Vista all major APIs are COM, you cannot avoid them unless you stuck in pre-historic Windows XP.

SDL on Android works, because Google has exposed the required APIs for game development to NDK, everything else requires a JNI call, or having Java code being called via Android IPC (binder).

Xamarin performance is quite bad versus Java/Kotlin, because they have to cross two layers to do anything meaningfull,

.NET => NDK => JNI and then for every Android event that gets triggered Java/Kotlin => JNI => .NET

Xamarin runs on ART (there is no JVM on Android) anyway, because the only kind of binaries that NDK allows for in proper Play Store applications are shared libraries loaded by Zygote process that owns ART.

Re:Swift and C++ interoperability workgroup announcement by sirwhinesalot on Wednesday February 02, 2022 @02:18PM (#30182558)
All of what you just said is true and yet at the same time utterly pointless to this conversation :| so no I do not get the point.

Yes to talk to the platform you need to use the platform ABI, whatever that may be. OS devs obviously can force people use these ABIs to talk to the platform, otherwise it is literally impossible to talk to the platform. That's true on Linux as well btw, if I want native Desktop notifications across Desktop Environments I need to use the freedesktop standard (based on D-Bus).

I can ignore that, same way I can create a pseudo "notification" with the old Windows API instead of the proper WinRT APIs (heck, even Microsoft Teams does), but that's irrelevant because it's not what we're talking about.

This discussion started from a post on Swift <-> C++ interoperability. Swift has a stable ABI. C++ could directly implement that ABI, but Apple does not have the power to enforce that.

So what point are you trying to get across to me?

Re:Swift and C++ interoperability workgroup announcement by pjmlp on Thursday February 03, 2022 @02:17AM (#30189638)
Teams is Electron junk using Web APIs for PWAs, again not C.

Apple has the power to enforce whatever they feel like doing on their own platform, that is why watchOS has a forked LLVM bitcode as executable format, regardless of how unstable the LLVM one is, C ABI matter zero there.

Just like they have a Safe C compiler for iBoot firmware, or have extended C language for lambdas (blocks).

Clang on Apple is different enough from upstream. that it warrants their own column on cppreference.

We are done here, there is no point getting across anyway.

Re:Swift and C++ interoperability workgroup announcement by sirwhinesalot on Thursday February 03, 2022 @04:50AM (#30190600)
I have no clue what you're on about and why you insist on telling me things I already know that are unrelated to the topic.

Indeed this conversation is not helpful for either of us, I still don't know what you're trying to discuss. If you're under the impression that I claimed OS providers have no power over their own platform, I never once claimed that.

I claimed they cannot enforce their crap as a standard outside of their platform (sometimes not even on the platform, UWP died for all intents and purposes), which is demonstrably true.

Were you under the impression that I was not aware Microsoft Teams is an Electron app? And how its old non-native notifications work and how Electron wraps OS APIs that aren't web standards exposed in Chromium?

Honestly you've not only repeatedly insulted my intelligence in this thread, but you've also ignored all of my attempts at having an on-topic conversation. Normally that would make me label you as a troll, but you've had many valuable contributions across this website so I'll just assume you had a shitty day and leave it at that.