Is Zig the Long Awaited C Replacement
erik-engheim.medium.com
erik-engheim.medium.com
Sure, it introduces some new concepts, comptime being the main one, but these are very easy to grasp, and you will immediately use them and understand their value.
It’s a replacement for C as the compiler doesn’t get in the way, but still brings you safety features that C never had: checked arithmetic, slices, checked pointer casts, tagged unions, nice error handling, better memory management, defer, etc.
It also has excellent support for WebAssembly.
I must confess that Zig is the first language that I’ve been so excited about since Rust.
On the other hand it’s a weird statement to make in an article about Zig, because Zig is much worse.
The language is huge though and getting bigger (which I don’t enjoy).
But compared to something like OCaml or Haskell, the syntax is just plain nasty for real fp. It feels clumsy and verbose - like wearing a pair of hob-nail boots for ballet dancing.
However, I think they made a good trade off in terms of balancing readability, power and progressive disclosure.
And I have coded Zig in a style that resembles Elixir code, which is basically: "use a different style for casing, never use the self syntactic sugar".
A lot of C programmers complain about the wildness of C++, but frankly, often complex C programs are that close to be their "own languages" with custom object systems, Macros and what not.
Essentially all of the most demanding, highest paid system development is done in C++. The number of C++ programmers, judging from attendance at rapidly proliferating C++ conferences and seminars, including even ISO 14882 SC22/WG21 C++ Language, is growing faster than ever. Attendance at each WG21 meeting in the last several years has exceeded that of all previous meetings.
By all means, learn niche languages. They bend your brain and make you a better programmer. Just don't imagine there will be a job waiting for you, using it. And, any code you write in it at work will probably be abandoned when you leave, as it will be too hard to hire a competent replacement who can maintain it.
This means that the niche languages to learn are the ones that really do bend your brain. Rust, OCaml, F# are probably worth exploring. Zig, by that measure, probably isn't.
The natural fate of almost any language is to die. Only a few get the miracle it takes to survive, as did (only) COBOL, Fortran, C, C++, Python, Java, and Javascript. There was a time when Pascal and Ada looked like they would survive, but the needed miracles did not arrive.
There is no excuse for writing a new program in C, or, for a program that should be maintained over years, any niche language. There is no reasonable expectation that a language will still be known, over the life of the program..
It is just barely possible Rust and/or Go will survive. If they do, over the years they will get radically more complex.
Most important would be to cut the number who try it, and give up before they have got their legs under them.
Changes in how borrow checker enforcement is applied could go a long way. But, even mentioning the topic, people come out of the woodwork shrieking that you want to turn off the borrow checker. It is a real embarrassment to the Rust community to have these people.
Another critical problem is the extremely slow compiler. It really should be two orders of magnitude faster. There has been extremely heavy lifting to get it just percentages faster, that will not be enough.
There is really no objective reason for a Rust compiler to be slow. C++ has unfortunate historical baggage that slows it down, but Rust doesn't have any of that. I don't see how to get a good compiler for Rust short of writing a new one. If an undergraduate class can all write a Java compiler in a semester, why is there still only one, slow, Rust compiler?
* * *
Go might survive if it can break out of its cloud niche. Cloud is trendy now, but trends change. Other languages have had much wider usage than Go has, and faded. Nothing about Go makes it uniquely suited for coding cloud operations. As it gets more complex, its ease of adoption and fast compilation will both become less pronounced.
Another thing I could imagine is an AI based garbage collector, so good that it is on par with manual memory management. In chess we have recently seen AIs (neural networks) taking over the lead and beating the strongest conventional engine (stockfish, written in C++).
The only place Go has a word, indirectly, is if we happen to make use of either Docker or Kubernetes.
My point here is that, this is already a big place and it's likely to increase, although Docker might be replaced sooner than later.
Java and C++ are already major for decades and don't go anywhere any time soon.
So.. perhaps be interested in it with a grain of salt.
That said, I don't know if I'm interested in Zig: I like comptime but passing around allocators doesn't seem pleasant I think I prefer Odin's 'context' concept.
C 0.24s (gcc 9.3.0, gcc $file)
C++ 0.96s (gcc 9.3.0, g++ $file)
Go 0.12s (go 1.15.2, go build $file)
Nim 0.44s (nim 1.2.6, nim c $file)
V 1.37s (v 0.1.29, v $file)
Zig 16.9s (zig 0.6.0, zig build-exe $file)
I guess Zig does some optimizations but not sure how to turn them off. Someone should do an analytical comparison for all of them and different values of N.*The generated V test doesn't work with the latest version as `;` between expressions throws a bad token error and zig requires `a` to be `.{a}`.
- Yes, fast compilation.
- Generics, templates or a decent macro system
- Defer, finally or scope based resource allocation
- Some way of handling coroutines (async/await or channels)
- Supports Linux AIX/x64, Win64, ARMv7/8, Darwin
- Tooling for OSX, Android, Linux, Windows
- Supports clojures, blocks or at least inline functions
- Supports ranges, lazy iterations, generators or at least yield
FPC actually comes close, except it doesn't have clojures coroutines or generics.
As much as I dislike it, fact is that C will stay around as long as UNIX clones, or POSIX implementations exit. So here efforts like Frama-C and Checked C are much more tailored for success.
Then if we look at the OS landscape, we already have a mix of languages replacing C to certain extent.
On mainframes, C was never relevant to start with, so the surviving ones from IBM and Unisys make a mixed use of their original systems programming languages, PL/S and NEWP, mixed mostly with C++. Whatever C like code is used, it is in the context of "compiles as valid C++" still.
On embedded, besides C + language extensions (stuck at C89 in some domains), there are C++ based platforms like Arduino and ARM mbed, microEJ (Java + C), Pascal and Basic (a couple of surviving OEMs are still around), Oberon (Astrobe), Ada, Java and .NET bare metal, TinyGo and Rust are now doing their baby steps.
The relevancy of C on embedded can be shown by Microsoft adopting it as the only language for Azure Sphere applications, despite the whole sales pitch about IoT security. Apparently despite all that talk, the market they are targeting is only interesting in buying, if the platform only speaks C. Currently developer requests for C++, C# and Rust support keep being nicely rejected.
On research OSes we have GenodeOS now going with a mix of Ada/SPARK and C++.
Windows is a mix of C, C++ and .NET, and the market pressure for C has won, as Microsoft gave in to their "you will only need C++ and .NET", and the more recent version of MSVC introduced support for C11 and C17. Most likely also caused by the need to support Azure Sphere and WSL development workflows.
On ChromeOS, only the Linux kernel is C based, everything else is a mix of JavaScript, C++ and Rust.
On Android, similarly, we have C for the Linux kernel and "legacy drivers", everything else is a mix of Java and C++. Project Treble also supports writing drivers in Java if one wishes to do so, and the reason why Google is having meetings about using Rust in Linux kernel is related to possible adoption of Rust in the Android world.
Fuschia uses a mix of C++, Rust and Dart. I think none of the original C code still survives as pure C.
Apple platforms use a mix of C, Objective-C, C++ and Swift.
Then we had experimental OSes like Redox (Rust), Powernext (D), in production hypervisors like gVisor (Go), secure firmware like TamaGo from F-Secure, low level file system drivers like Weka.io (D),...
C++ has gained a new wind on its sails as the main language for GPGPU programming, regardless of used directly or via language bindings. The only thing that might cut it, are better support for GPGPU programming from other languages, or some kind of GPGPU DSLs, then again NVidia is pretty much focused on first class support for C++ on CUDA, so.
Game console SDK are all about C, C++ and now C# (thanks Unity).
So, C replacements already exist, one just needs to step away from C cargo culture that there is no other systems language.
On the other hand, there are domains where no matter what, C will remain the sole king. So we need to fix its warts without throwing away everything.
However, Zig is a welcomed arrival to the party of compiled languages, maybe it will find its niche and make a couple of happier users anyway, even if it never makes a dent replacing C.
That I what I consider a success for programming languages, having their own customer base that keeps the language alive.
"Any headline that ends in a question mark can be answered by the word no."
So, good point. Zig must be a great C replacement.