Does Swift replace C/C++?

As I am starting projects ranging from Systems Programming to Cloud and AI, I am looking for a language that is modern, powerful, and has a long lasting future. My number one language is Swift, even if it is currently mostly used inside of Apple's ecosystem, I am looking at creating mostly everything from scratch, and for that I need mastery of language.

While doing research for such, the same two languages that are always advised are C/C++, but only because of how much software is already written in them, which again, is not something that I will even use.

I found on the official swift.org website that "The goal of the Swift project is to create the best available language for uses ranging from systems programming, to mobile and desktop apps, scaling up to cloud services." and "Swift is intended as a replacement for C-based languages (C, C++, and Objective-C).". I've never been happier to read something as such. But are these actually and practically true, or just a language design concept? Would I be able to write software ranging from OS to Cloud Computing, AI, Networking and such?

Thank you for your help and support.

P.S The use of Assembly is not a problem!

I think the question of "does Swift replace C/C++" is really two questions.

  1. Can I write programs in Swift that I would otherwise write in C or C++?

The answer to that is yes! Swift is a cross-platform, general-purpose language with good performance characteristics that try to balance safety, ergonomics, and performance (more-or-less in that order).

  1. Does Swift fully replace all the use cases that C or C++ cover?

As of now, no. Swift doesn't support many low-level primitive operations that would be required for doing certain kinds of systems-level programming. For example, Swift classes are atomically reference counted and heap allocated. Meaning, if you want reference semantics and the ability to perform side effects on destruction of an object, you must pay the cost of heap allocation and atomic reference counting. Swift also occasionally allocates on your behalf, primarily when calling into generic functions. Its standard library relies on being able to allocate like this, so many types (Array, String, etc) would be difficult to use in a system where full control over allocations is required.

The bottom line, though, is that people generally reach for a high level language like Python, Java, JavaScript, etc. when attempting to write cloud, AI, or network software, and when they hit performance issues, they tend to dip directly down to C or C++ for the portions that need performance control. Swift sits nicely between languages like Python and C/C++, offering a safe, fast, and native programming model that enables dipping down to low-level primitives and giving up safety when the high-level abstractions have too much of a performance cost.

The Swift-NIO project is a great example of a library focused on high performance non-blocking IO and is a great example of Swift's flexibility. The maintainers of Swift-NIO can probably speak better to the areas where Swift could improve.

8 Likes

Anddd, should you feel compelled to drop down, here's some resources Imported C and Objective C APIs. Though I gotta say that the ridiculous amount of inlining Swift compiler is willing to go diminishes the need by a fair margin, even with generics (especially with generics).