.NET 5 Swift Interoperability

.NET 5 introduces interop support for Swift. This (in theory) would allow libraries like WinUI to be used with Swift on Windows.

Has anyone ever tried using Swift with .NET 5? How could I use .NET 5 with my Swift programs, and how well does it work?

2 Likes

Looks like .NET 5 doesn't release until Nov. 2020, so it's unlikely anyone has used this, unless they're part of the development effort.

1 Like

Preview builds have been released. Granted, it's a longshot, but I was curious if anyone else had experimented with it.

I’m not sure I understand why this would enable WinUI usage in Swift. This enables swift usage in .NET. If you want the inverse (using .NET in Swift), the same amount of work as before needs to be done.

Could you please explain what you were suggesting, I feel as though I’m missing something.

I'm not super familiar with Windows, but I interpreted the following:

Objective-C and Swift interoperability will be supported on multiple operating systems.

To mean that Objective-C and Swift could use/be used by .NET (in the future) and by extension things like WinUI, since WinUI can be used in .NET applications. I'm assuming this interpretation is wrong, however, as I'm making a few assumptions.

The way that I’m interpreting this is different:

I highly suspect that they are referring to the GNU ABI ObjC, not the iOS ABI which precludes the ObjC interop.

The swift interop is interesting as yes, it would be possible to call swift from .NET, but that has no bearing on the WinUI aspect. It means more that it would be possible to bridge to swift and have that run some code, not necessarily have rendering in one and logic in another.

This would allow you to say replace Windows. Security.Cryptography with SwiftCrypto though.

If you’re just interested in doing UI development on windows, I’d recommend Swift/Win32. It already shows how to do UI programming on windows with swift (though, I may end up adding some C/C++ to it).

3 Likes

You could use it to write your backend in Swift and your UI in .net. However if you want to write your entire program in Swift, more work needs to be completed. Language bindings only work in one direction. It is very likely it doesn’t run outside the iOS or Mac runtime for .net. This code is usually very platform specific in .net. It may only be implemented for Posix platforms. It is still a nice development. Anything that allows me to use Swift in more places is great.

2 Likes

So would that mean even given this, it wouldn't be possible to write an application using WinUI with only Swift?


The reason I got interested in this is because a) WinUI is what newer apps are being made with, including some of Microsoft's own first-party apps, and b) I couldn't get swift-win32 to compile. I get the following error when running ninja -C build SwiftWin32 HelloSwift:

..\Sources\UI\Screen.swift:93:11: error: use of unresolved identifier 'GetScaleFactorForMonitor'
      _ = GetScaleFactorForMonitor(hMonitor, &dsfDevceScaleFactor)
          ^~~~~~~~~~~~~~~~~~~~~~~~
ninja: build stopped: subcommand failed.

Correct, at least that is my understanding. Perhaps someone else has a clever idea and can do some overly clever hacks to enable it.

Id like to know about issues with swift-win32 :slight_smile:

Interesting, I believe that this should be relatively old enough to support:

Which version of the toolchain are you using?

Microsoft has it's own Objective-C runtime, which could also be what they're referring to.

That's not the runtime, that's the frameworks. Have a look into https://github.com/microsoft/WinObjC/tree/develop/Frameworks WinObjC uses the GNUstep ObjC runtime IIRC.

Oops, you're right. According to their roadmap, they use GNUStep's libobjc2.

@Nobody1707 @IOOI - yeah, GNUStep ObjC2 implements the GNU ABI which is what I was referring to upthread.

I'm using whichever version was released about a month ago. I'm not sure exactly how to get which version number that is.

Ah, that was 5.2.

I bet that the recent improvements to the WinSDK overlay are missing. I would suggest that you grab a build from the nightlies for the time being or I believe that this should be a sufficient workaround:

curl -sL "https://raw.githubusercontent.com/apple/swift/master/stdlib/public/Platform/winsdk.modulemap" -o "%UniversalCRTSdkDir%\Include\%UCRTVersion%\um\module.modulemap"

This should be run form a Developer Command Prompt run as administrator as it is writing to the WinSDK installation.

For future reference, please file issues on the GitHub project.

1 Like

Wait would that eventually mean I could use Swift instead of C# in Unity3D? :innocent: Honestly asking, because I have no idea how language interop works.

2 Likes

You certainly would not be able to call unity from Swift. Maybe the other way. Unity uses its own fork of .net. They do a lot of crazy things to make a language designed for enterprise software work for a game engine. Unity built much of the game engine in a subset of c# that runs through a custom compiler called Burst that provides C++ like performance. Normal c# game scripting is translated to c++ then compiled with yet another compiler called IL2CPP. Game scripting is not nearly as optimized as Burst. Honestly, too much magic going on for my tastes.

Unity started out on Mac. In another world maybe they would have moved from C++ to Swift instead of C#.

1 Like

That‘s not what I meant though. I think it wouldn‘t be possible to have an app without C#. So it would be still great to use Swift from within C#.

Does however Swift interop in C# mean that theoretically we could have C# libraries to be interpreted in Swift? Just like we have ObjC libs with a Swift overlay.

Unfortunately that will never work with Unity. Unity isn’t a C# library. It is basically its own platform with a non-standard version of c# built on top of it.

Terms of Service

Privacy Policy

Cookie Policy