188 points · by todsacerdoti · 5y ago · 98 comments
- Continued progress on the new "stage2" compiler, which is written in Zig itself (long promised, work started in earnest I think about six months ago). There will be an optional non-LLVM backend this time, which is good news for build times. Even in debug mode, most of build time currently is spent waiting for LLVM.
- Incremental compilation with live reloading (in-place binary patching). The stage2 compiler is being written with this feature from the start.
- I can't find the link, but I believe the plan for stage1 (the current compiler written in C++, with all its warts) is to simply delete it. Since the new compiler will also support compiling to C, the devs will simply have stage2 compile its own source into C at some point, and that will be the stage1 from that point forward.
Here you go! https://github.com/ziglang/zig/issues/6378
P.S. Amazing work up to now! Kudos! I'm a fan just for everything it does what other language designers ignore, like designing the language to both compile and link fast. Cross compiling always available is a masterpiece. I guess that feature wouldn't have to be present on the platforms which don't have LLVM, if the language is to work on such too?
That is awesome! I’ve longed for such a feature when I work with Go and Rust although Go is usually fast enough that I can use a custom watch script.
I hope Zig succeeds in becoming a stable and minimal language. Few languages manage it, they tend to steadily bloat over time. The most obvious exceptions are C and Scheme, which are famously ascetic.
I think it's important to keep in mind that with C you might end up using a bunch of extenions, complex pre-processor macros or pragma magic to achieve essential things.
C in itself is relatively simple, but using it in practice can end up becoming quite complex.
Zig ends up being simpler in some ways by having less magic and special ways of achieving things. "printf" is very magical in most C compilers, but the equivalent in Zig is nothing particularly special from either the language or the compilers side.
I think the only thing that truly adds complexity without being strictly necessary, is the async stuff. But you can argue that async IO is becoming a really essential thing for systems programming, and using it without explicit language suppport is a nightmare.
1. https://github.com/bminor/glibc/blob/glibc-2.32/stdio-common...
Of course, even if you understand the rules you'll still accidentally write code that invokes undefined behaviour, which is much of the reason languages like Zig exist.
See whole section here
https://ziglang.org/documentation/master/#Undefined-Behavior
the difference is, they try to check for them at compile time, and if you compile with safety checks, also at runtime; however, if you compile with ReleaseFast (which I assume most people will in production), those runtime checks are turned off and the undefined behavior still exists.
You can’t really check if an integer variable is greater than 2^32, since that itself would cause an integer overflow.
Well if you include preprocessor, make, and cmake (which I think is basically standard now), the level of complexity feels "close to parity".
True, but on the other hand... C _does_ have preprocessor macros, which from a pure language standpoint adds a lot of complexity.
* Clojure (JVM): https://clojure.org/
* ClojureScript (JavaScript): https://clojurescript.org/
* Fennel (Lua): https://fennel-lang.org/
* Hy (Python): https://docs.hylang.org/
* Lisp-Flavored Erlang (BEAM): https://lfe.io/
And there's a similar growing trend of "strongly-typed functional language that compiles to X":
* Elm (ML inspired -> JavaScript): https://elm-lang.org/
* Elixir (Its own thing -> BEAM): https://elixir-lang.org/
* Fable (F# inspired -> JavaScript): https://fable.io/
* PureScript (Haskell inspired -> JavaScript): https://www.purescript.org/
* ReasonML (ML inspired -> JavaScript): https://reasonml.github.io/
* Teal (Lua with strict type annotations -> Lua): https://github.com/teal-language/tl
As well as the gazillion languages that compile to and interoperate well with C, such as Nim and several Scheme dialects (Chez and Chicken, probably others I'm forgetting). Oh and Haxe, which apparently can compile to like 10 different languages and I don't understand why it isn't more popular.
I like all of these languages because they don't require the user to switch to a different library ecosystem in order to use a programming language that they find more comfortable/enjoyable/powerful/expressive. I consider this extremely important because of how complicated some low-level protocols and systems can be, and reimplementing everything again for every new language is a waste of effort in my opinion.
Also, the proliferation of things that specifically compile to JS enforces a only-half-joking saying I heard a long time ago: JavaScript is a better compiler target than programming language. I think I've heard the same said about C as well.
Please accept pull requests that fix older example code, or please make the provided examples work on newer versions of zig.
Average PR lifetime in ziglang/zig repo itself is about 12 days. This is actually pretty fast compared to other projects. I'm proud of the fact that the oldest open PR in zig is from October 2, 2020 - only 2 months and 11 days old.
From the perspective of someone submitting a pull request, it's frustrating to not get a response within a day or two. I completely get that. But it's just not possible when you have a team of 1-3 people who are able to engage with PRs, and hundreds of people submitting PRs.
Since this project has started, the Earth has completed 1732 rotations, and yet 2877 pull requests have been closed. That's 1.7 per day, including weekends and holidays. Consider that for the first couple of years of this project, I was moonlighting it with a day job.
I'm not trying to throw shade at you, just saying, man, cut me some slack. I'll get to your PR.
Thanks for your reply and thank you for Zig.
I'll reopen that particular issue, and will try to fix any example zig apps that I can't compile, and will submit PRs for them.
Here is a talk on using Zig in exactly this kind of environment in production:
https://www.youtube.com/watch?v=124wdTckHNY
It also depends on your project's timeline. If your project will be stable in a year or two, that might well coincide with Zig's stability program.
If Zig's design goals suit your project then it might be worth investing for the long term and gaining deep understanding of the language as it grows.
I mean, FWIW, Rust is fairly ready for embedded systems, even when OOM-safety is essential. There are crates dedicated to providing fallible memory allocation primitives for exactly those use-cases.
With that said: I do believe Zig would work a lot better than Rust in severely resource-constrained environments. Rust has a few pitfalls that can easily cause a code size explosion (anything using core::fmt, for instance). And zig's constexpr appear more powerful than Rust's current stable const fn, which can be critical to writing maintainable, small code.
I've worked on a Rust reimplementation of a bootloader that had to fit in 8KiB of RAM, and I often went over accidentally by having a panic handler pull core::fmt. I haven't tried Zig for the same use-case (yet), but from a cursory glance, it appears to be less prone to this kind of code-size explosions.
The question of when will Zig be as mature as Rust is impossible to answer.
This is actually a surprisingly underserved field. There's relly very few language that actually tries to be just C but better.
Go: Uses garbage collection, not very applicable
Rust: Too complex, more like C++ but better. Bit too cumbersome to write "unsafe" code
D: More like Java/C++, although they have a "better C" mode, but feels like an afterthought
Nim: Uses garbage collection.. like D they're kind of trying to reach down into the plain C usecase, now with some nice reference counting stuff. Also relatively complex
Some actual attemps that I know of: C2, Odin
Some reasons why Zig is a better attempt at "C but better":
- Complexity of the language is just a little higher than C, and mostly in ways that resolves issues with C that leads to its own complexities (Like, the C pre-processor is simple, but solving problems with it can be surpringly complex. Or how you may have to resort to compiler extensions/pragmas to solve real problems)
- The Zig compiler is also a fantastic C compiler
- C headers can be imported and used directly in many cases
- Significant parts of the language is dedicated to excellent interop with C (even has syntax for 0-terminated strings)
- Cross-compilation is a highly prioritized use-case (C is very popular in embedded programming, where cross-compilation is essential)
That said, I am super excited about the Zig project, and it hits a sweet spot for me in a way that none of the other "C but better" projects have. Calling it another "C but better" language wasn't an attempt to disparage the project, but to classify it.
I’m of the impression that C will never die.
I've even plugged zig into another highly concurrent vm and gotten it to be a "good citizen": https://youtu.be/l848TOmI6LI (these constructs are running concurrently in test and CI so I know they are robust)
Mobile app dev?
PS: My daily work involves writing Java/Kotlin for Android, and just started learning Flutter for cross platform mobile app dev.
I mean, if one day Zig support those, that's definitely super awesome. Of course I'm aware that more menpower on that direction is needed :)
It's better to mention in which areas it really excels, where it beats the competition hands down.
* Things that would be in scope, but the Zig project is not yet capable of supporting. An example of this would be compiling stuff to GPUs, e.g. SPIR-V. Such things are planned but not implemented. https://github.com/ziglang/zig/issues/2683
* Things that will never be in scope. An example of this would be code obfuscation, which is a use case the Zig project does not recognize.
* Object formats that are drastically incompatible with the von Neumann architecture. It would be infeasible for Zig code to be compiled to such things. For example, attempting to compile to CSS (Cascading Style Sheets).
I think that's it. Anything else would be supported, provided there were enough people interested in providing maintenance for the respective parts of the codebase.
V, elixir, D, nim.
But zig seems to really the best of all of those, at least it's my favorite.
Any other similar language? (except go and rust, of course)
Maybe Crystal, Free Pascal even.
If Elixir is in, then Kotlin, Crystal and OCaml should be in as well. I don't think Elixir is similar though, not at all.
> Any other similar language?
I only think Rust, C, C++ and Go are similar enough, in order or similarness.