How it is to use Swift outside of the Apple ecosystem?

Go is my main programming language and I've been programming on it for quite some time. But I can't do any personal projects on it anymore because of the type system, it's just too simple to express what I want to express. On this journey to find a better programming language I've tested everything from Elixir, to Rust, and Scala FP.

I quite like Elixir and I think it has a lot of potential once the new set theoretic types are fully in place, but until them, it's just a promise.

Scala FP is absurdly complex. It's also a dying language, probably because of the complexity and the drama at the community.

Rust is quite close to what I want. Compiled, performant, good type system. The issue is that it's just too low level for my needs which tends to overcomplicate the code, hello borrow checker :waving_hand: .

Some friends recommended me to take a look at Swift, which I have always ignored because of Apple since I'm mainly on Linux and doing things for servers.

I'm just wondering about the usage of Swift on servers, if it works well enough, development on Linux, etc... Could Swift be the language that I'm looking for?

7 Likes

Sure, I say give it a go -- we've, in the Server workgroup been at it for quite some time and there's a lot of libraries and support for server use-cases nowadays.

Prominent use-cases include of "well known" things would include Thing's app's backend, Apple's own private cloud compute or password monitoring system. And tons of smaller and larger systems that I don't remember or don't know by name. OrdoOne are building a trading system with Swift as well, so it's even well suited for high performance things like that.

If you like elixir you'll find our notion of actors (distributed too), quite familiar; and gen_stage is like async sequences.

Fun fact, I used to work in Scala for years, and developer the prominent Actor system (Akka) of that ecosystem. Swift isn't quite Scala, but it's very close in expresivity if that's what you're after -- I'd say "just enough" perhaps :slight_smile:

6 Likes

I would also add that Swift is close to Rust in a lot of things, but uses ARC by default instead of a borrow checker, with consuming and borrowing keywords for opt-in ownership control.

1 Like

If you like rust - I would also recommend to check swift.

I kinda like rust in many ways with all its borrows and other low-level stuff. And maybe a bit a fan of it as a former c++ dev.
But in many cases it is just an overcomplication unless you actually need to do very low-level things.

Swift for me is a nice trade-off when you usually have really good performance while do not digging much into memory management until you really need it.

So you can model and build pretty much everything you need.
And only if you have very specific use-cases for performance or integration - you can go with low-level types in stdlib or use c/c++ interop.

2 Likes

It might: take a look at the Swift Package manager source, which is mostly written in Swift and builds as a Swift package on mac/linux/windows, plus Android too. :wink: For a library, swift-foundation is highly cross-platform.

You may want to look at Zig also.

I find Swift a good balance of native performance and comfy ergonomics. I’ve bounced off Rust because the language makes it too awkward to do anything but single-ownership, and lacks OOP.

I’m doing cross-platform development; the biggest issues I’ve seen on Linux are (a) large binary sizes and (b) poor documentation of the Foundation framework. Other than that it’s been working well.

1 Like

to be fair, there is a straightforward way to avoid both problems, which is to simply not use Foundation.

2 Likes

If you are starting from 0, I would pick a language that was designed to be cross-platform. Otherwise Swift is a decent choice, I have 3 different services running on my home server.

A lot of issues/PRs just get ignored for weeks/months/forever on Apple/Swift GitHub, you are better off posting issues in the Swift forum first. (Other language ecosystems have their problems too)

I have a template repo that I use (originally I wanted it to be generic but I switched to tailoring it just for myself): Cyberbeni/MultiArchSwiftDockerfileExample: This is an example on how to set up building a multi-architecture docker image using the new Swift Static Linux SDK introduced along with Swift 6.0. - Codeberg.org

(It is using a custom SDK, see: Huge memory usage increase when upgrading from 6.2.3 to 6.2.4 with Static Linux SDK )

There are good quality projects out there for running a web server, using various networking protocols, and server side rendering HTML. Here are the ones I use:

You can easily wrap C libraries if you are missing some small functionality that already has a C implementation: Cyberbeni/swift-utf8proc: Swift wrapper for JuliaStrings/utf8proc - Codeberg.org

2 Likes