top | item 11060282

Introduction to the Zig Programming Language

98 points| AndyKelley | 10 years ago |andrewkelley.me | reply

44 comments

order
[+] blt|10 years ago|reply
Cool project. The "more pragmatic than C" design goal reminds me of game developer Johnathan Blow's Jai language [1]. It seems like Zig is still in very early stage, are you planning to incorporate any Jai-like features? I especially like its SOA keyword for quickly changing data layouts [2].

I really like this corner of the language design world. People always say that C is too entrenched and none of these languages are compelling enough to make a large number of developers switch, but I disagree. Someone said that C and Lisp are two local optima in the space of programming languages... I think that Lisp probably is, but C is only near a local optimum. There is a true local optimum remaining to be found.

[1] https://github.com/BSVino/JaiPrimer/blob/master/JaiPrimer.md

[2] https://github.com/BSVino/JaiPrimer/blob/master/JaiPrimer.md...

[+] AndyKelley|10 years ago|reply
I wrote this elsewhere in response to a similar question:

> I've been following the project, and he has some great ideas that I plan to shamelessly copy such as Struct Of Array support. Running arbitrary code at compile time is on the table, but I haven't figured everything out about it yet. Jonathan Blow is an interesting character. I have a lot of respect for him, but I don't always agree with him. I feel like his perspective is insightful but also limited in some ways. I'm sure the same is true for myself, but all that is to say, our respective languages will be (and already are) different enough to warrant being different projects. For example, one of my goals is "friendly to open source package maintainers (such as Debian)". This entails things like keeping a meticulous record of how to bootstrap the compiler without compromising security, having a reproducible build, providing ways for the maintainers to configure various things, etc. Based on what I know about Jon, he'll either be completely apathetic about package maintainers, or potentially even have a hostile attitude.

> Also no spoilers for The Witness please. I'm waiting until it comes out on Linux to play :-)

[+] lobster_johnson|10 years ago|reply
Looks very interesting. The author is apparently a contributor to Piston [1], which is written in Rust, and the language seems to share some similarities with Rust.

[1] http://www.piston.rs

[+] AnimalMuppet|10 years ago|reply
> At the end of the day, all that really matters is whether the language helped you do what you were trying to do better than any other language.

Absolutely. But one problem that new languages face is that libraries are part of how much the language helps you do what you are trying to do. So to gain any traction, a new language needs to have libraries...

> Complete C ABI Compatibility

... and there they are! Nicely done, though this is a fairly common solution these days. Or, it could be regarded as a cheat, because you don't have your own library...

> Alternative Standard Library

... and another point of criticism dies.

I also like the difference between debug and release builds. All in all, this looks pretty nice.

[+] zvrba|10 years ago|reply
There are many new C ripoffs, but nobody seems to acknowledge Cyclone (https://cyclone.thelanguage.org/) which is a lot older. It was even backed by a corporation (AT&T), but never succeeded. I wonder why.
[+] AndyKelley|10 years ago|reply
Maybe it has something to do with this:

> Cyclone is no longer supported; the core research project has finished and the developers have moved on to other things. (Several of Cyclone's ideas have made their way into Rust.) Cyclone's code can be made to work with some effort, but it will not build out of the box on modern (64 bit) platforms).

[+] dalailambda|10 years ago|reply
The thing I like C for is the simplicity. Writing C after working in a very verbose language for a while is refreshing. That said, my few wishes for a C replacement would be generics, methods, and namespacing. The last two are to avoid funky naming schemes everywhere. Maintaining the proper low level control and simplicity would be a top priority though.

Overall this project looks interesting. I'm interested to see where it goes in the future and what design trade offs end up being implemented.

[+] lsd5you|10 years ago|reply
I don't really understand this point of view. Is C not (very) verbose? Even if it's just because of the manual memory management.
[+] isaiahg|10 years ago|reply
Looks pretty promising actually. I love the methodology laid out for the language. I just wish more languages would move away from the C syntax. While we're all used to that syntax I think Python has been more successful in demonstrating the practicality of whitespace for readability. And considering that readability is a major factor for this language I think it's a shame it doesn't use whitespace more effectively.
[+] nordsieck|10 years ago|reply
The big downside to using whitespace in this way is that if you are trying to generate python, especially from another language, it's a nightmare. Explicit delimiters make meta-programming much easier.
[+] Solarsail|10 years ago|reply
I wonder how feasible it would be to decouple data size from type & overflow semantics, a la this comment:

https://news.ycombinator.com/item?id=7103347

[+] AndyKelley|10 years ago|reply
The comment you linked is insightful and the commenter is indeed in the target audience for the language. One point I didn't understand is this: "decoupling of on-the-wire layout from in-memory layout". What does that mean?

This is good stuff, and I plan to work on some embedded projects to experience firsthand the needs of this kind of project.

[+] PeCaN|10 years ago|reply
Correct me if I'm wrong:

Does this mean something like (pseudo-C):

    struct weird_bit_field {
      uint8_t x : 32;
    };
i.e. an 8-bit int that takes 4 bytes of space? Or am I misunderstanding something?
[+] gnuvince|10 years ago|reply
What is Zig's position on memory safety? Rust is, correctly IMO, memory-safe even if that can cause some discomforts when programming (I hear this is temporary, and mostly subsides as one gains a better understanding of lifetimes and borrows). On the other hand, Jonathan Blow's Jai eschews language-guaranteed memory safety in order to allow the programmer greater freedom, but does nothing to prevent use-after-free, double-free, null deref, etc. (Edit: this is based on my understanding of his language as it was in his last demo. I don't know what plans he has for the future, but I think that with him calling Rust a "big idea" language and rejecting that philosophy, we should not expect Jai to do much to prevent incorrect manipulation of memory.)

If we take those two languages as the extremities of a spectrum, where would you put Zig on that spectrum?

[+] adrusi|10 years ago|reply
Rust optimizes for correct memory management. Its borrow checker and safe/unsafe distinction makes the easiest option to make most often the one that is safe.

Jai optimizes for performant memory management. Several of its features, such as SOA pointers, make organizing your data to take advantage of the processor cache easier if not as easy as managing memory in any other way.

It looks like Zig optimizes for simple memory management. Without all the bells an whistles the other languages offer, the easiest way to manage your memory is the most obvious way. Which is good from a prinicple-of-least-surprise perspective.

[+] AndyKelley|10 years ago|reply
Closer to Jai. That's all I really have in response to the question though. It's a bit of a simplistic measurement. To get a better feel for the answer you could browse the source code to the Tetris game implemented in Zig linked in the article.
[+] sjrd|10 years ago|reply
It's quite funny how much the debug vs release builds resemble the fastOpt and fullOpt stages of Scala.js. Except that fastOpt actually produces decent runtime performance thanks to incremental whole-program optimizations. But time spent too compile and undefined behavior handling are very much the same.
[+] panic|10 years ago|reply
Is there a plan to add a generic resizable array type? That's probably the number one thing I miss in C.
[+] AndyKelley|10 years ago|reply
Good question. I'm going to see how well this works as part of the standard library, since it would have to allocate memory.

And then operations like append or resize could return an error (since memory allocation can fail), and I want to make sure the semantics for that are reasonable.

That and a Map data structure I want to make sure have convenient semantics since these primitives are used in many software applications.

[+] sova|10 years ago|reply
It's cool. Do you tinker in any lispy dialects?
[+] AndyKelley|10 years ago|reply
(I (tried (once (in (college (but (lisp (is (not (my (style)))))))))))
[+] m6w6|10 years ago|reply
This looks awesome, but, oh, the irony...

A language to replace C implemented in C++. ;-)

[+] adrusi|10 years ago|reply
Note that all major C compilers — gcc, clang and msvc (also icc, if that counts as major) — are written in C++.
[+] giancarlostoro|10 years ago|reply
Plenty of languages that have been used instead of C for a while have been coded in C++. I don't see the issue since he's using LLVM, which I believe is a wise decision.
[+] vmorgulis|10 years ago|reply
Github says it's C++ because of the .hpp/.cpp extensions but if you look at the code you will see it is more C than C++.

Maybe it is because of LLVM build system?