Maintain It with Zig
kristoff.it
kristoff.it
> soon we’ll also have a package manager
This. If they succeed to do this (and I have my doubts) it will be a paradigm shift for low-level "system" programming.
Let's hope this is not vaporware.
Finally, it's relatively easy to contribute to Zig. Andrew Kelley is very opinionated on where the language should go with an emphasis on simplicity which sometimes can make things awkward but he is also persuasive and welcoming. I have been using Zig for a month and I am still positively surprised so he must be onto something.
[0]: https://github.com/ziglang/zig/blob/master/lib/std/json.zig#...
There's definitely something to that analogy, but I think it also misses a lot of really important details. For example, Zig has generics, which right off the bat makes it hard to say that it's "like C". Also Rust enforces memory safety, which isn't like C or C++.
Of couse, it's a much less powerful feature, there's nothing like interfaces or stuff, just a way to "overload" based on the type.
The only preprocessor part in the example is mapping the function-like macro 'cbrt(X)' to the language keyword '_Generic(X)'.
I wrote a blog post about comptime if you want to learn more: https://kristoff.it/blog/what-is-zig-comptime/
I feel like Rust wants to replace C but it also wants to replace C++. And given the complexity difference between the two languages, that means that Rust will end up closer to C++ than to C.
So there's some overlap based on how Rust positions itself, but not based on how Zig positions itself.
Rust wants to make systems programming memory safe (and type safe while at it). It's not really about any specific language, it's about dragging the field forwards on the safety front.
Well it's not really Rust, it's the Rust community, which kinda wrestled it away from Graydon Hoare: the original inception of Rust was more of an applications language (what with the evented model and the split stack and the planned-though-never-really-implemented GC'd pointers) — which explains part of the historical confusion with / comparison to Go.
But then a critical mass of people took a look at this early rust and figured "we've got plenty of memory-safe and type-safe (ish) languages for applications, but this thing has precise control over memory and lifetimes and shit, we could actually build reliable infrastructure with that", and it coincided with a lot of memory safety issues news (which has been continuing ever since), and the rest is history, pretty much.
The ZIG compiler requires a C compiler to compile to a native executable though.
Do you mean compiling the zig compiler or zig programs?
I think neither is quite the case. zig itself can build both Zig programs and C/C++ ones. Zig doesn't need to use a C compiler to build native Zig executables. It does need a linker, as does Rust, as do C/C++ programs and AFAIK zld is not yet ready.
However it's accurate to say that it's only able to compile the C/C++ code by leveraging libclang, the C/C++ compiler underneath.
By the way, check out this project by Zig core team member, Vexu: https://github.com/Vexu/arocc
No it doesn't? Not unless you want to also compile C, which the Zig compiler is capable of doing because it bundles clang.
I'm imaging some poor software engineer in 2065, having to maintain an enterprise legacy stack... levels of FORTRAN/COBOL->C->C++->ZIG->RUST->PERL->PYTHON.... all the way up....JS..etc..
I have good news and bad news: That programmer will not know any of those languages, as they will just program in English and GPT-3000 will convert it into code.
GPT-3000 will have no problem understanding some interpretation of your English (or Spanish or Mandarin) "program", but will it be the right one? How long will it take to get GPT-3000 to understand what you mean when you can only talk to it in English (or Spanish or Mandarin) ?
A piece of software is a set of instructions that tell a computer how to do something. We use languages like C, Python, Haskell and Lisp to describe what to do in ways that avoid as much ambiguity as possible.
The question is: if you used a natural (human) language, how much confusion would (a) the putative GPT-3000 (b) another programmer (c) a compiler/interpreter incur when trying to understand your instructions?
Of course, the complexity might explode from there…or you might just not really notice it all that often. Anybody familiar with LLVM or WASM or heck even a language that can transpile to JavaScript can probably relate to this possibility.
The thing that’s harder to fathom is going from low-level to high-level. High-level -> intermediary low-level -> high-level is fathomable though.
I do, because that would mean that more attention must be paid to make languages work together, instead of each language ecosystem becoming its own little island. Maintaining a project built from a dozen languages (where each language does one thing well) should be just as simple as maintaining a mono-language project.
> Zig is a general-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
Keep up the good work, I'm excited to see where Zig goes next.
The biggest difference in the problems they solve, in my view, is that Zig does not aim for safety. It'll be easier to write safe programs yourself with Zig than it might be with C, but it doesn't give you any guarantees.
Zig also seems like it may be a better fit for embedded programming; there's a big community around embedded Rust, and it sounds like there's a lot of progress, but still an uphill battle.
But yes, Zig:Rust :: C:C++ has merit in that Zig is a much smaller, less feature-rich language.
The issue with this analogy is that it assumes that C, one of the languages used in the most diverse set of circumstances ever, means the same thing to everyone. Same with C++, frankly.
I don't think that's the parallel I would draw.
Rather, I think both Zig and Rust aim to serve the use cases C and C++ do, but Zig and Rust pick different points on the tradeoffs involving safety. Zig feels like a "better C", in the sense of bringing modern language features to C, but it chooses safer rather than safe. Rust supplies modern language features as well, and chooses to prioritize safe; sometimes that comes at the expense of other factors, such as productivity or compile time. I personally prefer the point on the spectrum that Rust chose, but I think Zig still offers improvements over C.
One thing that impressed me about Zig is how simple the language is. I get the impression that it would take very little time to properly learn the language and become effective at it.
Rust, on the other hand, has a whole lot of complexity even without the ownership semantics. So much to learn and understand. And, in some cases, work around. It seems, from looking at others' code, that there is a very strong temptation to lean on `unsafe` instead of figuring out how to solve a problem within the constraints it normally applies.
That has me wondering if there's really all that much safety difference in practice? Simpler code is generally easier to understand, and it's generally easier to find and fix bugs in code that's easier to understand. I can imagine a world where it's a tradeoff between, "You definitely won't have any of this relatively narrow category of bug, unless of course you opt out of the static checks, which you will probably do sometimes, even though we tell you you shouldn't," and, "There are no particular guarantees, but you're generally less likely to have any of this broader category of bug."
Some people do believe that, let's say "conceptually parsimonious" languages (using complicated words to describe simplicity is amusing to me, sorry) do exactly what you say. Others believe that complexity inherently exists, and you can put it in the language, where a compiler can tirelessly check certain properties for you or a runtime can do magical complicated work on your behalf, or you can put it in the programs, where users have to check such things themselves.
Another way I heard this expressed one time is Ruby vs Python. I once heard someone talk about how they preferred Python as a language more, because it was simpler, but enjoyed actually programming in Ruby more, even thought it was more complex, and they value simplicity. The reason is that all of that nasty ugly stuff Ruby lets you do makes you be able to make extremely nice APIs. Things you couldn't do, or just folks don't do, in Python. And therefore they ended up liking using Ruby more in practice.
I suspect that this is a topic that we, as a profession, will debate about endlessly.
I personally would not agree with
> Languages with more powerful abstraction abilities bring joy to the writer but make things tedious for the reader.
I find map/filter to be easier to read than a C-style for loop. Things that are conceptually denser make discussion between experts easier, even if it makes things harder for non-experts. This is why jargon exists, for example.
In short I believe languages that one can pick up in a couple of days and then read code where reasoning of the code is highly local would stand a higher chance of being perceived as simple.
Programmers notoriously conflate "simple" and "easy" (classic talk: https://www.infoq.com/presentations/Simple-Made-Easy/) and so I believe languages that are easy for a lot of programmers will also be perceived as simple, whether or not that's accurate.
I think we'd all agree there's a spectrum, with, say, Assembly on one end and Idris on the other, where neither Zig nor Rust are near either extreme, and are actually rather close to each other. It's not a matter of accepting or rejecting a principle, but on picking slightly different sweet spots (hopefully) on a tradeoff spectrum.
Rust doesn't check at compile-time most things that could be checked at compile-time. In fact both language check almost the same things at compile-time, and almost the same things at runtime. The design differences come down to one or two things that are clearly tricky and that neither language "does for you" -- i.e. you still need to think about them -- but are, indeed, checked in different ways.
Are you sure those examples of unsafe Rust code were written using unsafe in order to work around safety constraints due to difficulty?
I ask because there are some very real and valid use cases for unsafe code in Rust that have nothing to do with the fact that writing the code safely might be more difficult.
The big bias I'm working from here is that, as an outsider, I have a hard time reconciling all the big talk about how Rust makes it impossible to experience certain classes of bugs, with the existence of a known set of things that its type checker can't check, and which includes a lot of things that are difficult to avoid. One could be forgiven for thinking that the Rust community has a habit of keeping its fingers crossed behind its back when talking.
I don't want to make the perfect the enemy of the good, of course. I realize the feature is there for an important reason. But it sure looks to me like Zig is also good despite being imperfect. Perhaps they're both similarly good languages.
It's far worse than that, the problem is that the machine can't know what was intended. The result 5 from calling sum([1,2,1,1]) makes sense, but alas this function was intended to produce the product not the sum, the inputs were supposed to be [1,2,3,4] and the desired result was 24... the machine can't hope to guess that.
A program may be buggy even if it its meaning is transparent and exactly reflects its effect, simply because it doesn't reflect the intention of the person who wrote the wrong program.
This quote is attributed to Babbage:
'On two occasions I have been asked, – "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" ... I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question'
But that is what we wanted, even if we obviously can't have it.
https://doc.rust-lang.org/book/ch19-01-unsafe-rust.html
The spec here is to turn one mutable slice into two non-overlapping mutable slices. The operation does not violate memory safety; we wind up with only one mutable reference to any part of the object. But the compiler does not know how to verify that the programmer has done this correctly. Here, 'unsafe' means 'I certify that this is safe, use that as a lemma to do further checking'.
It is a big difference to have it on your face that something might need to be properly double checked and having each line of code as possible security exploit.
Would you consider Oberon to belong in the former or latter category? On one hand, there's no explicit code blocks as such (syntactically). On the other hand, unsafe primitive operations are limited to the SYSTEM module, so functions calling operations in the SYSTEM module can be considered doing unsafe things.
Just like Modula-2 already does it.
And just like languages with unsafe code blocks, there are compilers that have switches to forbid the use of tainted modules unless explicitly allowed.
In any case, way better than what C, C++ and Objective-C offer, regarding security per line of code.
char* c_foo(char* bar) { ... }
simpler than fn rust_foo<'a>(bar: &'a str) -> &'a str { ... }
?On the one hand, c_foo is shorter and doesn't use strange symbols. On the other hand, rust_foo doesn't leave you guessing on whether the returned value needs to be free()d or not, and whether the heap allocation backing `bar` can be free()d after the call or not.
fn rust_foo(a: &str) -> &str { ... }
but Arnavion is trying to be explicit here to make a steelman.)The only thing I've found being a pain point is C's biggest pain point: the lack of a proper string type. Even though I've worked for years in the past with C and C++, I still get tripped up, and in Zig I keep on being confused whether I should use raw byte arrays, or sentinel-terminated arrays for certain string manipulations. Maybe I'm not familiar with common idioms in Zig that might help avoid this C-style tracking of string/buffer lengths malarkey that I tend to end up with where a slice is not appropriate.
Maybe a dynamic-length string library is the way forward.
I've always tried as much as possible to treat strings as just opaque data and never look into them, which tends to work well, but in some domains you really need to look at and massage the characters/codepoints/grapheme clusters, and the lack of a first-citizen UTF-8-aware string type is, I think, a bit unfortunate in this day and age. I understand having one of those could make C interop a bit gnarlier (I think Odin's approach of having two separate string types -- one of which is just meant to be used for interop -- should be workable).
Oh, and the ecosystem of libraries is still young. I needed to format timestamps as human readable date/time to a file, and had to step down to C to do something that basic[1], but it'll get there in time, I'm sure.
[1]: "basic" from the perspective of the end-user of the language. Having written a (not even fully featured) date/time library in C++, I consider date and time to be one of the hardest domains I've ever had to work in.
You don't need a string type for that, you just need routines that handle UTF-8 strings, like utfcpp (https://github.com/nemtrif/utfcpp).
Like in any language with any lib that is not self-contained (cuts across all aspects of a program and can be used anywhere) and has many different implementations and no sanctioned one.
However, if the philosophy here is to be unopinionated and optimize for flexibility over everything else, I feel I can't really fault them for making that choice.
Then again, I'm not sure exactly what a type would be for Unicode. Basically just a flag that the array of bytes has been checked and is valid? It's nice to let the type system help you enforce your application boundaries, but I think you could wrap it pretty easily in a struct of your own if you needed that.
[0] https://github.com/ziglang/zig/blob/master/lib/std/unicode.z...
In something like Zig I think it's OK that it's merely a stated assumption that these bytes are UTF-8, not actually checked - so long as people take that seriously.
It's nice in code that maybe isn't very concerned with such things to be able to know that any "string" is actually text we can display, output to a console, something like that. Not for example a TCP/IP packet we haven't decoded yet, or the first 32 bytes of a JPEG image.
Programmers who aren't writing firmware for a vacuum cleaner or washing machine, probably want string literals like "this" in their code, and you'd naturally want those to have a type, for which a string type is the obvious fit. I believe in 2021 this type should obviously be UTF-8. "It's just some bytes" is far from useless, but it isn't much of a string type.
I was sceptical at first about Rust's choice to build in str (immutable UTF-8 string slices), because that seems like a relatively high level concept. But unlike std::string::String, str isn't that tricky after all. See, the only place such things would come from (not having std::string::String to make new ones) is the program source, and our compiler is necessarily already reading the program source, so it follows that the compiler must know how the source is encoded etc and thus it does actually know exactly what those literal strings are. If your literal wasn't Unicode, that wasn't a Rust program and it won't compile.
And also as long as all supported operations on those bytes still result in utf-8 strings (there could be unsafe operations too, like splitting at a random byte offset, but there should be a clearly marked safe set that wont corrupt a valid utf-8 byte sequence).
I have this internally in my lang AST Rust
Utf8
I think that is the type: struct City {
name:Utf8
}
For a scripting lang like mine I surface it as `Str` but in a system language I think is better to say `Utf8`Well, as you said "Unicode is quite large and complicated".
And in 2021 it's not just anglosaxon software users anymore (if they ever were), good unicode lib if not type is a must.
I was doing some pretty gnarly high-level filesystem work (transforming an unstructured tree of files/folders into a more structured tree following certain naming and categorization conventions -- a lot of parsing/building/concatenating paths). I have been working with higher-level languages for the past decade or so, but have had a wanderlust to get away from garbage collected langauges and go back to my roots a bit, so I've been comparing some of the newer options in the "improved C"/"C++ replacement" space.
It may very well be that I'm not the target demographic for Zig, and that's totally fair, but to me it felt pretty jarring to go back to working with C-style character (byte) arrays and all the (well-known) issues with which they are fraught. I didn't feel like Zig was an improvement over C in the string handling area (as opposed to all other areas), whereas languages like Go and Rust are.
Other than that, I felt Zig was a joy to use.
Rust's OsStr is my favorite approach so far. It stores Linux's raw bytes as-is, and stores Windows's possibly-valid UTF-16 as WTF-8. This makes path management "just work", with the ability to operate normally on invalid UTF-8 or UTF-16 paths, and zero-copy conversion from UTF-8/ASCII strings to OsStr (though converting OsStr into UTF-16 requires parsing). (Qt's QString-based file dialogs on Linux fail to convert invalid UTF-8 paths like those in https://github.com/petrosagg/wtfiles into QString, causing Qt-based apps to open/save the wrong paths.)
However there are difficulties in printing an OsStr. For example, a file dialog that shows filenames as raw bytes can't show non-Latin/Unicode characters in a human-readable form, and a file dialog that shows filenames as Unicode strings can't handle invalid Unicode filenames. GTK3 file dialogs show filenames as Unicode strings, and when encountering files with invalid Unicode names, instead displays "file�name.txt (invalid encoding)".
Worse yet, how should a file dialog allow users to rename files? If it's based around byte arrays, the user can't enter Unicode characters directly, and if it's based around Unicode (or a locale-specific text encoding), it can't display existing files with invalid Unicode/etc. in the name (probably not an issue if it allows the user to rename to a valid name), nor allow users to enter invalid Unicode (which is not an issue IMO).
APFS is utf8, HFS+ is utf16.
> with valid Unicode glyphs
That doesn’t really mean anything.
Apple’s fs do guarantee the paths are correct, as in, valid whatever encoding this has nothing to do with glyphs.
APFS also does not perform any normalisation while HFS+ uses a custom variant if NFD. While HFS+’s normalisation has its issues and critics, APFS’s lack of normalisation is probably worse: https://eclecticlight.co/2017/04/06/apfs-is-currently-unusab...
For a minimal std, maybe Zig can have a standard `str` type which is used for all functions that don't need to grow the string and the `String` type is provided by any 3rd party library which is expected to provide a conversion function from `String` to `str`.
These strings types could or could not have a requirement of being Utf-8. Maybe there is a builtin wrapper to convert them. So you can any dynamic byte array which can be converted to `str` (just a pointer and length, no validation) which can be later converted to `utf8` (str but validated). A `String` which guarantees Utf-8 could directly convert to `utf8` to save validation costs.
I sincerely approve of the skepticism. I can assure you there is already a very large fire under my ass to get this shipped.
To provide some more context, here is a snippet from the latest release notes[0]:
> Having a package manager built into the Zig compiler is a long-anticipated feature. Zig 0.8.0 does not have this feature.
> If the package manager works well, people will use it, which means building Zig projects will involve compiling more lines of Zig code, which means the Zig compiler must get faster, better at incremental compilation, and better at resource management.
> Therefore, the package manager depends on finishing the Self-Hosted Compiler, since it is planned to have these improved performance characteristics, while the Bootstrap Compiler is not planned to have them.
I can't help but mention, I am incredibly excited about how well the self-hosted compiler is coming along. Progress is swift, and all the ambitious design decisions (fully incremental compilation[1], extremely fast debug builds[2], low memory usage, multi-threaded) are intact. I anticipate it to make quite a splash once it becomes generally usable.
I don't expect the package manager to take nearly as long to develop.
[0]: https://ziglang.org/download/0.8.0/release-notes.html#Packag...
[1]: https://vimeo.com/491488902
[2]: https://twitter.com/andy_kelley/status/1416485475125141504
I'm going to check out Zig because of your response.
I love coming across comments like this one on HN.
I wrote about cross-compiling to a raspberry-pi and writing a barebones driver for an OLED display[^1]
That was a lot of fun despite the fact the code was incredibly basic. The interop with C was effortless.
[^1] https://www.kamelasa.dev/posts/gettin-ziggy-with-it-pi-zero....
I feel Elm's growth has been stunted because the creator is actively hostile with whoever disagrees with him. I remember the infamous "leaving elm" post which was handled so poorly by Elm's author.
If they had handled it well, elm would have definitely seen a large uptick in adoption
There's very little evidence of ongoing hostility from Evan - I pay attention to his work and writings and find he's rarely if ever hostile. In fact he's very mild-mannered, and is painstakingly detailed about explaining the reasons for why he does things the way he does, over and over again.
Perhaps that wore thin once or twice in the face of repeated criticism and demands. We've all seen way worse.
Yes, sticking to his vision has excluded ideas and contributions, and made some people very angry. There have been several high profile blogposts and twiits painting him as some kind of despot. And when other overzealous developers swooped in to defend his work, it just fed the meme, one which people on HN like to repeat every chance they get.
Well, I for one am glad he's a 'code despot' - considering how many people keep demanding that they dilute Elm's guarantees for the sake of JS library interop or marginally less boilerplate. Any bending to these demands and Elm today would be just another framework with a slightly different syntax, with none of guarantees that allow for the innovative tooling and libraries [1] we see coming out of the community.
Whether you agree with Evan's vision or not, Elm is doing just fine meeting its stated goals and is a fantastic and delightful technology and toolchain.
So please give over with trashing other people's hard work. If you don't like it, just don't use Elm.
[1] for example, the compiler's dead code elimination capabilities, elm-review's tco detection and other amazing auto-fixes, lamdera, etc.
The criticism is valid. Haskell is painfully strict and, still, it offers various ways out of its guarantees. Rust is painfully strict and gives you `unsafe`. Neither of them break your build if you don't host your code in a specific github org.
Limiting certain kinds of js access to vetted libraries is benevolent, if that's part of the language's goals. Likewise the lack of type classes or mutability/unsafe escape hatches.
Elm's developers do not enforce constraints to be 'hostile' or 'far from benevolant'. It's a tradeoff - language power for certain guarantees, guarantees that not even Haskell can provide.
Unfortunately, this sometimes means trying to constrain access to the completely unconstrained js library ecosystem, so the solution, at least for now, is a bit of a blunt weapon.
Note that Elm doesn't 'break your build' if you don't use github - that's a constraint on linking to official libraries only. I don't use github and my code works just fine.
However, elm is not pragmatic. the issues mentioned in Luke plant's "leaving elm" [0] are valid. and Evan's response to that was far from satisfactory[1] and this has affected Elm's popularity.
The disagreement between BDFL and community has happened in many other cases as well. and there is a way to solve them amicably. Most recently, Vue faced it when community caused uproar over composition API RFC. and Vue solved it nicely and amicably[2]. If Elm has followed similar approach, Elm too would be praised for it, and gained even more popularity.
[0] https://lukeplant.me.uk/blog/posts/why-im-leaving-elm/
[1] https://www.reddit.com/r/elm/comments/g070sz/evans_response_...
ahem
> Progress is zig ...
Thanks for all your work! I ported a bunch of basic data-structures from C++ to Zig very early on (right as y'all began exposing generics). Even though the code no longer compiles, still worth learning The Zig Way™.
To make things a little more interesting though, Zig's shape in this regard should be roughly congruent to C's, so you should be able to do anything with Zig that you can with C. This includes linking against shared objects for C libraries like, e.g., SDL2[1] or building mixed-language Zig+C projects[2].
0: https://ziglearn.org/chapter-3/#packages 1: https://dev.to/fabioarnold/setup-zig-for-gamedev-2bmf 2: https://tiehu.is/blog/zig1
https://github.com/ziglang/zig/blob/master/CONTRIBUTING.md
Though I’m interested in hearing from the creator itself as well.
If I may, my only piece of feedback (as someone who looked at Zig with fresh eyes) is that the syntax has lots of special chars (@, !void, .{}, &, etc). Is there a rationale published for these and other design choices (even if it's "I like it that way"). I would have expected a language with ambitious goals like Zig to take a stab at coming up with a grammar that's even simpler and cleaner than C.
Regardless, getting a new programming language working, noticed and used is monumental effort, so I wish you best of luck with Zig!
Relative to c, though there are imo fewer special chars:. Zig adds ! and ?, Which are there for really good reason, but subtracts for example <> and #.
The hardest thing is the . syntax for anonymous structs and tuples, but to be fair it is very consistent, and so many times I'd think,"hey I wonder if..." And then try it with the syntax and of course it works.
That is sadly my gripe with a lot of the newer languages too. I have been looking for something to replace C for a long time, something that makes it easier to not shoot yourself in the foot. I just find most of them significantly less readable than C.
Rust seems to be going down the route of C++, hence I do not deem it a safe language at all, as I have an exceedingly hard time parsing the source and figuring out what is going on.
Zig is WAY better in that regard, but as OP mentions: all the special characters make things more difficult to parse. Calls like these (taken from the introduction page) contain so much visual clutter:
try expect(@call(.{}, add, .{3, 9}) == 12);
I sadly doubt any of this is possible to change at this stage anymore. Zig definitely seems the most promising C replacement with regards to tooling, especially cross compiling and C interop are a breeze. Truly inspiring work!The only new C-like language that really nailed it for me syntax wise would be Nim. Very clean and easy to reason about, which - from a programmer perspective - makes it exceedingly safe to program in. It just seemingly lacks most of the safety guarantees that the compilers of other, newer languages provide.
How will you verify the chain of trust?
One thing I wish the blog post had brought up is testing! Zig has some great tooling around tests, and that is something sorely lacking in a lot of C projects. If nothing else, you can pretty easily use Zig to simply write some tests for a C project! Even that would be a big improvement to the ecosystem.
I feel like the notoriety that C++ gets for it's absurd complexity comes not from variadic template metaprogramming/rvalue references/unique_ptr or whatever, but the absolutely humongous effort needed to understand automake/CMake/autotools just to build other people's code (god forbid on a different platform than the original author).
And even better since they support binary libraries, I can focus on my own code instead of building the foundations first.
I'm working on a game engine in Zig[0], and I've been able to package up GLFW, write a build.zig file that `git clone`s all of the third-party system dependencies so that anyone can just:
const glfw = @import("glfw/build.zig");
...
lib.addPackagePath("glfw", "glfw/src/main.zig");
glfw.link(b, lib, .{});
And have GLFW building (and cross-compiling!) for their project, without installing anything other than Zig and Git. No XCode. No `apt-get install ...`. Nothing. Just `zig` and `git` binaries. Zig is the C compiler and builds the GLFW source, and I have repositories with the required prebuilt system libs for cross compilation.Well yes, that would kill me too, if I actually expected the language to come with a single blessed dependency that addresses functionality <Foo>, that I will never want or need to patch the blessed dependency, and that there would be only one way to actually build a program or library.
But it doesn't kill me because I don't expect either of those things.
The dependency stack for my (21 year old) project consists of 86 3rd party libraries that on an x86_64 platform take up about 2GB after compilation. This is intimidating and sometimes painful, but I don't find it insane in any way. We have patches for a half-dozen of the deps that will never make it upstream and we have to be sure that we compile the libraries in the precise way that we need them (for example, we require the thread-safe version of libfftw, not the regular version).
But the stack of stuff it takes you supposedly need to get stuff to compile is often insane. On one project I demonstrated that just find . | grep ".c$" | xargs gcc ... and a final link ran significantly faster than the mass of recursive nested/fake/etc makefiles that were supposed to speed up the compiles by doing dependency computations. I remember showing someone the basic "we turn the .c files into .o files, and then we make a second pass to link 'em" and they were like "really? that's all there is to compiling a C program?"
The real straw for me when I was contributing to the Cairo project was recognizing I was going to have understand this stuff more, and so I sat down and read the Make manual: 16 chapters. Now I need to understand Automake, read the manual: 26 chapters. And then Autoconf: 21 chapters. It was like it was just spinning bigger and bigger.
I have written small applications in both. To be transparent: my total dev hours in D are probably somewhere around ~100, and in Zig about ~30.
So I am more familiar with D than Zig, though competent enough with both to have written a few small real-world programs.
> Its interoperability with C at the source and object level is first-class
D has direct interop with both C, and C++. Also, it's recently gained it's own C compiler, and can natively compile C code so that they are D objects, not just external definitions.- https://dlang.org/spec/interfaceToC.html
- https://dlang.org/spec/cpp_interface.html
It can even directly interop with C++ classes:
- https://dlang.org/spec/cpp_interface.html#using_cpp_classes_...
> the syntax was very easy for me to pick up as someone familiar with C/C++
D is closer to C/C++ than Zig IMO. One of it's design goals is actually: "10. Where D code looks the same as C code, have it either behave the same or issue an error."
And: "The general look and feel of C/C++ is adopted. It uses the same algebraic syntax, most of the same expression and statement forms, and the general layout."
See: https://dlang.org/overview.html#goalsI found Zig to be much less syntactically similar to C/C++ than D. Zig felt more like a mix of Rust/Go.
> the features it adds on top of C (e.g. slices, compile time execution)
D also has slices and (outside of Lisp) was the first programming language I believe to introduce CTFE and CTFE-based metaprogramming. C++'s implementation as I understand it is essentially lifted from D.- https://dlang.org/articles/d-array-article.html#introducing-...
- https://tour.dlang.org/tour/en/gems/compile-time-function-ev...
> its translate-c utility works amazingly well at transpiling C code to Zig
As stated above, not required due to ImportC, but there are several tools to automatically generate "extern" bindings to C/ObjectiveC/C++.You use these if you're linking with an external out-of-tree library, or a C++ library.
- C, Limited C++ = https://dlang.org/blog/2019/04/08/project-highlight-dpp
- C, Extensive C++ = https://github.com/Superbelko/ohmygentool
- C, Objective-C = https://github.com/jacob-carlborg/dstep
> its test blocks make unit testing trivial to integrate.
Again, D also has this:- https://tour.dlang.org/tour/en/gems/unittesting
=============
Not trying to start a pissing match, because I actually think that Zig has a lot to offer as well -- but for these particular list of things, there's no clear winner here.
The bit about D is that while you CAN write it to look like C, (or even directly embed ASM blocks in it), it's usually written in a higher-level form without pointers that looks more like JavaScript:
// Count line length of stdin
void main() {
import std.range, std.stdio;
auto sum = 0.0;
auto count = stdin.byLine.tee!(l => sum += l.length).walkLength;
writeln("Average line length: ", count ? sum / count : 0);
}
----
import std.algorithm, std.conv, std.functional, std.math, std.regex, std.stdio;
alias round = pipe!(to!(real), std.math.round, to!(string));
static reFloatingPoint = ctRegex!(`[0-9]+\.[0-9]+`);
// Replace anything that looks like a real number with the rounded equivalent.
void main(){
stdin
.byLine
.map!(l => l.replaceAll!(c => c.hit.round)
(reFloatingPoint))
.each!(it => writeln(it));
}Zig on the other hand seems like a silver bullet aimed straight at C's heart, the embedded space. Once it arrives it'll only have to worry about Rust.
Rust is the black hole at the center of the software universe, slowly sucking the blood out of everything else. Zig vs Rust is shaping up to be an epic battle, like the Jedi vs the Empire, for sure :)
> To me it's the clear choice if I'm going to rewrite an old C project since I can mostly just copy paste the old code and it runs
With the new ImportC feature you can literally copy-paste the code and reference your C types from D directly with a mixed compilation.
https://news.ycombinator.com/item?id=27872596
https://dlang.org/spec/importc.html
Quoting Walter from the thread linked above on it's purpose/reasons (which I think ought to go somewhere official tbh, since that brief discussion in the above thread is the best + most succinct overview of it currently AFAIK)
The reasons for ImportC are:
1. It's no longer necessary to translate .h files to D to interface with C. While doing the translations isn't hard, it is tedious, and if there are a lot of .h files it becomes a barrier.
2. .h files get updated over time, and correspondingly updating the D files is error prone. It's much more effective to just compile the new .h files.
3. It is not necessary to even create a .h file - just compile the .c file with ImportC and D will access it just like any other module. D will even inline the C functions in it.
4. Use it as a standalone, small and fast, C compiler.headline from that website: Zig is a general-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
Zig is a system programming language, whereas when someone says general purpose programming language they mean java or python. And "robust, optimal, and reusable software" - this type of words are used by all projects these days that it sounds like MBA speak.
I also don't like the tendency of system language projects (zig, rust) to market as if they are application languages, perhaps in an attempt to drive adoption. Can't blame them for this, but it seems an influx of relatively young, inexperienced programmers from ruby rails / javascript crowds creates false impression of a strong ecosystem.
Zig's cross compilation stuff and general compiler features, that this post talks about, is excellent, awesome, and makes me quite jealous frankly.
I actually got annoyed with this enough that I started doing a quantitative analysis; if you look at the canonical repository tracking this, it's got 44 total issues, most of which are jokes https://github.com/ansuz/RIIR/issues If you search GitHub for issues with these words in it, you get some, but many are either obvious jokes, people making issues on their own projects to think about doing this, and things like that. I never followed through on collecting it into a blog post though.
I didn't think your writing was inappropriate at all; memes are memes, and I'm convinced that this one is just never going to die, because it's taken a life of its own, regardless of the underlying truth or not. It is always worth pushing back on this sentiment, even if I don't think Rust specifically tends to actually embody that sentiment very much.
(Also: I don't know why you're now being downvoted. Hacker News works in mysterious ways.)
As to why it used to matter, well, it is tremendously difficult to bring a new programming language into popular usage, and even more in the systems space. It requires tireless effort by a large number of people. It is a very fragile thing. The stories we tell ourselves and others matter, and they influence what happens.
That's all I'm willing to say right now :)
https://news.ycombinator.com/item?id=25198571
Someone reposted the same document with Rust replaced with "$hotlang" which, for a reason you can guess, wasn't flagged at all:
https://news.ycombinator.com/item?id=25208313
My impression is that there are more than a few Rust developers who dearly believe in rewriting in Rust and were sincerely, bitterly offended by a joke to the contrary.
I could/would also get into a debate about "satire" and what exactly it is and means, but I have complicated feelings about it, how words change over time, etc.
I've found it be to pretty useful in determining why I'm much more sensitive to things I care about than the things I don't, and particularly when jokes carry an element of truth that I cannot bear to acknowledge
They call this evangelist.
I rewrote 'leakdice' as part of learning Rust, so arguably that doesn't count (though I had never rewritten a program to learn a previous language), and 'misfortunate' wouldn't make sense in unsafe languages so that doesn't count either, but I then also immediately began rewriting 4store in Rust.
I had never been compelled to rewrite stuff in other languages I learned. I am a compulsive programmer, so I did write stuff in languages I learned, (the other day I re-discovered, via an enquiry to a very old email address, that I used to know Scheme, as evidenced by the fact I wrote some Scheme that they were asking about from last century) but I didn't rewrite anything until I learned Rust.
C. Go. Python. Java. PHP. Bash. Perl. C#. Early C++. I'm sure there are many more. But always new things, either because the language afforded something I hadn't considered before or more often I wanted something and I chose a languages I knew would be able to achieve it. That's why I wrote some PHP less than a year ago, I'm not proud of that, but PHP got the job done so that's what I did. I can report that modern PHP is merely a bad idea, like Javascript, which I probably forgot to list above.
Anyway with Rust I not only wanted to write new low-level code in Rust, I looked again at C programs I use and thought, "I should rewrite that in Rust". It felt like a good idea, and I expect to follow through on it at least somewhat, let's see how long 4store takes.
And I'm one that will be super-happy if all C/C++ code in the world dissapear now and be replace by Pascal/Rust/Zig/D/anything else that is better.
MOST of my troubles are because C/C++. And the worst ones. Even if I 'm in a environment (business apps, ERps) where in theory system lang concerns are a distant worry.
"I think he said, 'No way?! Zig for the win!'"
In some way, you could say that Zig is pulling system programmers towards high-level programming, whereas Rust is pulling high-level programmers towards system programming. That's not a watertight comparison but I think it's an insightful one.
I guess the other (simpler) alternative is to wrap Zig into a Cargo package, similar to this:
I have been talking about how much I would love this to happen, but I can't do the work myself. Frankly, I don't contribute to the compiler, so even getting up to speed would be a ton of work I just don't have the time for.
* The collection of headers for various targets, and the tools to upgrade/maintain them.
* The source files for various libcs, patches, instructions/tools to upgrade/maintain them
* A declarative set of instructions to create build artifacts (not Makefiles!!)
* Instructions/patches needed to integrate Clang driver frontends into a different binary (making the "main" functions co-exist peacefully).
Would be a fair amount of overhead to collaborate, but perhaps could ultimately save both parties labor.
and that's ignoring the nightmare of cross-compiling with gcc.
andrew kelley has talked about this[1]. his main point was about the number of dependencies you end up with compared to zig, where the standard library includes a high-level build system api. so your zig build scripts are written in ziglang and run by the zig compiler. no extra dependencies.
(embrace, extend, extinguish) for those not familiar
Explore, Expand, Exploit, Exterminate
Second, "extend" has traditionally meant "extend with proprietary code". Think early 2000s Microsoft with browser APIs and J++, or Amazon with their in-house forks of MongoDB, PostgresQL, etc.
There's plenty of innocuous activity that could be called E3 if you stretch the definition that far. Most of the GNU ecosystem for example.
While Zig is manual memory managed like C and nearer to the core, but harder to program.
"Nim is a statically typed compiled systems programming language."
As someone else pointed out, when using Nim with its ARC/ORC and move semantics (which will eventually be the default), it's closer to Rust than to Go.
That being said, Nim's current default GC does well for many kinds of workloads. If it's really causing a performance problem, it's possible to disable it and manage memory manually (or use a different language for the task at hand, of course).
I don't do C/C++ development. How many headaches would using Zigs compiler & build system solve?
Truth is that cross compilation always breaks down in complex deployment use cases.
I think the problem for Zig is that they use MinGW as their Windows target, and the latter's standard library isn't really UWP compatible (yet?).
Zig uses LLVM for cross compilation, and so does clang. It's really not any better.
But sure, to Zig's credit, they ship all the supported toolchains and so don't have many different cross compilation variants in the OS's package manager. But that's a bit like buying one of everything, even though you'll only use one or two.
Clang doesn't bundle a C standard library implementation.
> But sure, to Zig's credit, they ship all the supported toolchains and so don't have many different cross compilation variants in the OS's package manager. But that's a bit like buying one of everything, even though you'll only use one or two.
Zig only bundles the sources, so it builds the C library on-demand. IIRC it's smaller than most cross compiler toolchains.
... So? It can use most any you wish.
> Zig only bundles the sources, so it builds the C library on-demand.
Which it then caches, post-compilation, taking up binary space on disk comparable to if you just downloaded the pre-compiled target binaries.
I don't understand the point of this comeback. If you have the artifact cached it means that you compiled for that target, while the rest of the stdlibs remain in source form (also Zig deduplicates header files which is why everything fits in a 40mb tarball), and you didn't have to download anything manually. What else would you want exactly?
People without incentive usually do not do this tedious kind of work, since "it works for me".
First, Zig has a pretty unique and impressive build&caching system. It makes something like this a lot less painful than it would be otherwise. It makes it practical to just download a 40MB tarball and get full cross-compiling from it.
Second, clang itself isn't in the business of building a distribution of itself + third-party libraries. Usually developers using clang get such libraries from their "vendor" (package manager if on a Linux distro, Apple if on MacOS, etc.). And those vendors naturally focus on their own platforms. So Zig ended up the first to really do the work to build a cross-platform C/C++ toolchain.
Not true. Unity once supported their own language based on Boo, a python dialect for the .net runtime, that they falsely advertised as "Javascript". That has since been depreciated.
My understanding is that `zig cc`/`zig c++` are thin wrappers around LLVM, so doesn't that just reintroduce the same problem?
clang cross compilation is not really any harder, you just supply the -target flag.
Yes, but it doesn't bundle and automatically compile the appropriate C library for the platform. Zig does.
> If zig cc is built on top of Clang, why doesn't Clang just do this? What exactly is Zig doing on top of Clang to make this work?
> The answer is, a lot, actually. I'll go over how it works here.
[1]: https://andrewkelley.me/post/zig-cc-powerful-drop-in-replace...
The point about the Python package example is not to say that Zig can get on platforms where Rust can't, but rather that the C infrastructure that we all use is not that easy to replace and every time you touch something, regardless of how decrepit and broken it might have been, you will irritate and break someone else's use case, which can be a necessary evil sometimes but not always.
This is a point I made in the section of the blog post dedicated to explaining the limits or rewriting code.
To some extent. Rust, due to relying on LLVM, is limited by its available targets; and so cannot enjoy the full access that C/C++ can. The same is true for Zig, however, since it does not compile to C.
As much as I lean on thinking Zig will achieve it first it seems unfair to make claims about things that haven't been done in a language that hasn't even been finalized as reasons it's better than a stable language in the same boat at the moment.
So if you can't use Rust because it doesn't have the broad compiler support that C does, you almost certainly can't use Zig either for the exact same reason. And then the article touts Zig using their own in-house linker, which seems inevitable to have less platform support than LLVM will? Zig isn't wrong for doing that, but you can't claim that's a benefit at the same time the problem with Rust is it's lack of fringe system support... :/
ZLD can link for M1 while LLVM can't yet. And Zig can also use lld alongside it to increase the platform coverage.
> but you can't claim that's a benefit at the same time the problem with Rust is it's lack of fringe system support... :/
I find it hard to believe that a dispassionate reader could misinterpret to this degree the goal of a sentence written in a chapter dedicated entirely to showcase the limits of rewriting code in another language, be it RIIR or RIIZ.
See my reply to the parent comment for the intended meaning of that example.
The author is not under any such impression. The key point is that Zig can cross-compile C/C++ code, while Rust can't. If you have a Rust project that depends on C code, you will have to put in some work if you want to compile it for a different target.
That's a pretty big difference in practice. The point about the Python package example is not to say that Zig can get on platforms where Rust can't, but rather that the C infrastructure that we all use is not that easy to replace and every time you touch something, regardless of how decrepit and broken it might have been, you will irritate and break someone else's use case, which can be a necessary evil sometimes but not always.
This is a point I made in the section of the blog post dedicated to explaining the limits or rewriting code.
> since it does not compile to C.
Give it some time, it will.
> If you have a Rust project that depends on C code, you will have to put in some work if you want to compile it for a different target.
Rust is built on LLVM, and so its toolchain can also cross-compile C/C++ code. There are crates for that[0], and projects inspired by that idea[1].
> Give it some time, it will.
Given enough time mountains become valleys. Zig doesn't compile to C now; and won't be mature and field-tested for a decade or more.
"This crate calls out to the most relevant compiler for a platform, for example using cl on MSVC."
But why do I care? I don't use rustc directly, the build system of choice does. And very few of the major build systems have an issue handling multiple languages.
Cargo (rust's build system) supports build scripts and the community has already created C/C++ compiler hooks such as https://github.com/alexcrichton/cc-rs
rustup and cargo also provide easy cross-compilation support, too.
I quoted from cc’s readme above.
I haven't had this experience with any other language. When a package manager lands, you won't even have to install any SDKs by hand anymore.
Is this a matter of popularity or features ?
With that said, I've heard the situation has improved since then.
For example, one of the biggest claims has always been fast compilation. Here's V compiling itself in 0.3 seconds: https://www.youtube.com/watch?v=pvP6wmcl_Sc
V was also self hosted (written in V) from the start, which says a lot about the maturity of the language.
> Combined with all the delays
There were no delays. The project was announced to be released in June, and it was.
By the way, we now have a cool OS written in V, that can already run Bash, g++, and V itself!
How could that possibly be true? You need a V compiler to compile V but none would have existed "from the start".
You've also said multiple times on Discord that the original V compiler was written in Go.
Of course initial version was written in Go, but it was never released.
On home page, the very first claim:
> No null
However V has `nil` or any reference can be initialized to `0`. How is then claiming `no null` not false?
It's on the 0.3 roadmap, high priority.
I see you created this account to spread false information about V:
> Ditto same experience, I asked if somebody could explain me the memory model of V; and all of sudden hell broke loose. I was asked why do I need to know how memory model worked, are you a troll, if you don't contribute to the lang you're not welcome here. Never asked a second question again.
This never happened. Otherwise you could send a link to the discord conversation.
No one gets banned or called a troll for asking questions.
Besides, the memory model is explained in details on the home page.
I like Kristoff from what I've read of him. He seems like a leader who will take responsibility and not shy away from hard decisions.
On that quality alone, my money is on Zig winning systems programming over Rust in 10 years.
Rust also has good C FFI story and honestly I think we will still see a lot of important code rewritten in Rust.
That said, a more ergonomic C which seamlessly replaces the often bloated build configuration of C/C++ projects while allowing a far nicer language to slowly replace the existing code feels like TypeScript for JS (which I consider a good and smart thing). Definitely agree it's going to get far more use than I'd originally have guessed.
[0]: https://adventures.michaelfbryan.com/posts/how-to-riir/
Furthermore, LLD doesn't support incremental compilation. It's just not fast enough for our compilation performance goals.
Zig may strike a better balance than Rust does here, but it seems more like Rust stole its lunch. I don't know why I'd migrate from C/C++ to Zig instead of going all the way to Rust?
2. Graph memory patterns in Rust are completely unsafe.
3. Code might leak and you dont have tooling to check your dependencies. Trusting the test coverage does not help you there with Rust, because Rust tooling is infeasible for testing leaks.
4. CTFE is not easy to track in Rust, so you dont know if certain code may have run-time cost or not.
5. Not all UB during comptime or runtime is (planned to be) checked/checkable https://rust-lang.github.io/rfcs/3016-const-ub.html
2. Wrap up the unsafety in a library like petgraph and use it. Or just use petgraph.
3. jemalloc's leak analysis works with Rust, as does Heaptrack.
4. With const generics it's trivial to pass a value through a const generic parameter to ensure it was evaluated at compile time. Right now that only works for scalars but that limitation will be relaxed later.
5. Most Rust code is safe code which is free from UB. Dynamic checks of all unsafe-code UB is infeasible. So what?
But anyway, none of these are reasons to use Zig.
2. petgraph looks like they only allow allocating and freeing in the same order and no deallocating of segments (think of a graph pointing to another graph) dynamically. So even if you wrap it, you cant arbitrarily deallocate and allocate things upon semantic conditions.
3. It would be more helpful to use static analysis or making functionality with potential leaks similar to have this information on code review instead of trying to test all code paths.
4. True. I think that works then. Looks like between Rust and Zig "const equals comptime".
5. "Dynamic checks of all unsafe-code UB is infeasible." It is only infeasible, if you dont have an idea on the memory model during comptime. The RFC also writes "In particular, this would mean we need to decide on an aliasing model before permitting raw pointers in CTFE.", because Rust is not settled on that yet. On the upside, this speeds up compilation, lol.
2. The big picture here is that for graphs and other data structures, you figure out a safe API for the data structure and write a library that hides the unsafety behind that API (or just use a library that someone else already wrote). You get a clean separation between a small amount of unsafe code and a large amount of safe code. This is the Zen of Rust and it's why "Rust can't express <XYZ> safely" is almost never an important issue in practice.
3. Rust's ownership and determinstic destructors mean you really have to go out of your way to create a memory leak; "just forgot to free it" doesn't happen. A memory leak has to be something nontrivial like a global data structure that is added to but not removed from, or a reference-count cycle that isn't broken. There aren't really good automatic static techniques for detecting these AFAIK (and I have a background in static program analysis) --- you would need something that models heap states over time, like shape analysis, which doesn't scale. As far as I can tell Rust at least as strong as any other real-world non-GC language at preventing memory leaks, certainly stronger than Zig which lacks ownership and destructors.
5. This is an issue I don't know much about so I'll leave it.
FWIW the endgame for unsafe code in Rust is formal semantics for unsafe code, proof obligations that guarantee the unsafe code preserves Rust safety invariants, and proof-assistant tools that let you formally prove the safety of your "unsafe" code. There's a lot of work to get there but that work is well underway.
Zig is super exciting, but without a GC or Rust type system, I’m worried about going “back” to a language where I have to think about memory leaks, use after free and so on.
You can have either extensive static analysis or fast compile times. Tracking ownership down to semantic analysis (where type layouts are in resolved form) requires adjusting and slowing down the complete compiler.
There are also known techniques like Type-After-Type that can avoid use-after free in C and C++ [0]. And also advances in hardware like ARM MTE [1]. Those things - and GPA, and not reusing 64-bit pages - have various tradeoffs in terms of overhead, but Zig is a developing language that has a chance to explore possibilities that C and C++ can't. So this is an interesting area to follow.
[0] https://dl.acm.org/doi/10.1145/3274694.3274705 [1] https://security.googleblog.com/2019/08/adopting-arm-memory-...
Seriously. You get these tests for free with zig, no extra tooling, if you use testing allocator in your tests. This also is a carrot to get you to write tests. Hell, I even mocked libc's malloc/calloc/free to make sure that my code doesn't leak memory:
https://github.com/ityonemo/Primes/blob/ee81d05e80d68854ab11...
> It is the Zig programmer's responsibility to ensure that a pointer is not accessed when the memory pointed to is no longer available.
https://ziglang.org/documentation/0.8.1/#Memory
Mocking malloc you say?
https://docs.microsoft.com/en-us/visualstudio/debugger/crt-d...
It exists at very least since Visual C++ 5.0, and Borland had similar tooling since their first Win16 compilers.
Yes, your examples are two proprietary, nonstandard techniques. Or alternatively using a third-class toolset (jemalloc toolkit e.g ).
Sanity is having a single, anointed way to do it in the stdlib.