29 ms·
Zig, the Small Language
- manaskarekar 4y agoI maybe overstating it, but another contributing factor to its growing popularity may be the documentation. It is really nice and well presented. Reminds me of how the rust book also welcomed you to explore the language.
- 3836293648 4y agoZig has good docs now? Nice. Last I looked Zig had some of the worst docs I'd seen to the point where I just looked at the std sources and eventually just gave up
- anonymoushn 4y agoMaybe language docs vs stdlib docs. Language docs are good for most topics, stdlib docs are much worse than stdlib sources.
- frou_dh 4y agoThat's exactly right. When I tried it, the Language Reference was great, and the stdlib documentation was a UX-challenged semi-broken webapp.
- kristoff_it 4y agoFor what it's worth, work on it as resumed now that the self-hosted compiler is mature enough. Still very far from being complete, so we don't link it yet on the front-page, but it's being actively improved.
- TakeBlaster16 4y agoI tried Zig recently but I found the unsilenceable lints to be a huge productivity killer. I actually posted a link to the GitHub issue this morning. https://news.ycombinator.com/item?id=32751317 https://news.ycombinator.com/item?id=32751317 This makes a normal workflow with `watchexec zig test` basically impossible, since before I can even run the tests I have to spend time hunting down which variables are used/unused at the moment and (un)commenting them. And it seems like they're planning to double down on this by making you adjust even more trivial things like public/private and var/const before it will even compile your code. https://github.com/ziglang/zig/issues/335 https://github.com/ziglang/zig/issues/335 https://github.com/ziglang/zig/issues/224 https://github.com/ziglang/zig/issues/224 Maybe I'm a bad developer, but my code is never perfect the first time around. I always spend time experimenting and refining my designs. I want to find a design that works, and only then spend time polishing and prettifying before I git push. I do understand the reasoning (they don't want people committing poor quality code), but this implementation just seems completely backwards to me. It breaks the natural order. It's like saying we won't let you ctrl+s until your tests pass to make sure you don't commit broken code. Stop nannying me and let me get my work done!
- ArrayBoundCheck 4y agoThat was my problem with it. That and being mildly annoyed the creator hates tabs
- LAC-Tech 4y agoThankfully the tabs thing is not meant to be permanent.
- j16sdiz 4y agoYou can always do ' _ = your_varible', don't have to comment individuals
- avgcorrection 4y agoThat was the first suggestion that Kelley posted on the issue and judging by the reactions people weren’t happy with it.
- masklinn 4y agoCompletely unsurprisingly. "Add useless garbage to work around the stupid decisions we impose on you" is not very appreciated.
- flumpcakes 4y agoIt is not useless though. There is a big difference between an unused variable and explicitly defining an unused variable. Kelly is designing Zig to do nothing surprising or change things underneath you. Even C does things that are surprising. If you tell the compiler "Hey, I know this is unused but I am going to write it anyway" then the compiler _can_ make decisions such as eliding the variable. I think we do all agree that it is sensible that a variable in code that is not used should be a compiler warning (if it is an error or not is clearly debatable).
- 4y ago
- purim 4y agowhy should I use Zig coming from Python/Go ?
- stephc_int13 4y agoFast code and fast compile time.
- purim 4y agohmmm but how much of a performance gain is there from say CPython or even Go? The syntax of Zig appears to be quite user friendly but not sure if there are hidden pitfalls. I am excited for Zig but careful in adopting new languages but if this takes off, what sort of changes might we see? Cheaper C/C++ programmers?
- stephc_int13 4y agoI'd say at least 2x-5x with Go and 10x or more with CPython, depending on the type of work.
- stephc_int13 4y ago>if this takes off, what sort of changes might we see? Cheaper C/C++ programmers? Zig is very likely to appeal to people who loves C, or newcomers who are looking for performance. The quality-of-life gains might lead to marginal changes in productivity/cost, but I would not bet on it.
- com2kid 4y ago> hmmm but how much of a performance gain is there from say CPython or even Go? It depends on what you are doing. If your python code is mainly calling out to highly optimized math routines, well, not much. If you are doing lots of computationally intensive work, quite a bit. If your Python code does a lot of allocating of memory, throws around lots of large objects, or has to parse a lot of inputs, then moving to a native language can be very beneficial. > I am excited for Zig but careful in adopting new languages but if this takes off, what sort of changes might we see? Cheaper C/C++ programmers? Newer more powerful languages tend to result in more complex software being written. Ignoring CPU and memory limits, no one would have been capable of writing a modern AAA game with the tools available back in 1992.
- Kukumber 4y agoZig indeed is pretty nice, i just wish it had some more sweet to it - my math type with + - * / overloads - simpler way to fill an array, i can never remember the syntax, it doesn't feel natural `[_]u8{0} * 10;` - smarter type system, i am tired of casting everything twice A good language is not a language set in stone, a good language is a language that doesn't make me feel like i have to suffer because they made a stupid decision years ago and they refuse to make it better
- wyldfire 4y ago"In the beginning the [operator overloading] was created. This has made a lot of people very angry and been widely regarded as a bad move." But in all seriousness that's pretty much antithetical to zig's goals regarding explicitness.
- Kukumber 4y agoif we can do 1 + 1, we should be able to do vec2 + vec2, same with mat4 * mat4 Odin proved it that it can be made efficiently while keeping sanity
- stephc_int13 4y agoThe thing about operator overloading is that it was abused during one of the worst era of C++. And vec2 + vec2 can probably be unambiguously translated to assembly code while mat4 * mat4 is an other story. In most cases it should be a function call, and not a trivial one, with SIMD it can be relatively fast but still several orders of magnitude slower than 1 + 1. (we're talking about more than 500 scalar-equivalent operations) And unfortunately, from my experience, if you let people (especially new coders) use this kind of powerful syntactic sugar they tend to ignore the performance characteristics because it look so simple and basic, like if the CPU had a special instruction to multiply two 4x4 matrices. I prefer when the function call is explicit, it is a bit more cumbersome to write, but there is less hidden complexity.
- 4y ago
- jstrong 4y agowhy do people care about binary size? I have never understood this. Disk space isn't free but the size of the binaries on my machine doesn't seem like a big problem to me in that regard.
- mbrubeck 4y agoIt can be critical when targeting microcontrollers with RAM and storage measured in kilobytes rather than gigabytes.
- zahllos 4y agoYes. For example the Arduino 328P ("Uno" boards) has 31.5k of program flash available to you, while the ATMega 2560 has 256k (minus again 0.5k for the bootloader). The STM32F411RE, a Cortex-M4, has 512k. In embedded development you can forget the overhead due to binary formats such as ELF (while the toolchain might output these as an intermediate, what is flashed is just the relevant sections), but if you are doing things like loop unrolling all over the place, you're going to get less functionality for static program space. This is why ARM and PowerPC have "embedded" variants of their ISAs. What this means in practice is a compressed form of their instruction representation. Why? If you have compressed instructions, you can fit more of them in program flash. So both Thumb and VLE are variable length encodings. Thumb, for example, can use 16 bits for many instructions, i.e. two bytes, whereas ARM by default would simply take four bytes for all instructions. (For the avoidance of confusion, they still "address" 32-bits of memory and are thus still 32-bit microcontrollers). PowerPC's VLE is similar. The driver here is cost. You could put in more program memory (the address space has plenty of room) but that costs more money, and when you don't need it, why do it?
- paines 4y agoAt first I had the same thaught, but then he speaks about Arduino and embedded systems with hard constraints, and it makes sense tho.
- lifthrasiir 4y agoI think it is more of a proxy to the controlability, which is not often the case with C/C++ where a seemingly small stdlib feature triggers a larger dependency and you can't easily get rid of them.
- kbd 4y ago> It’s a very appealing language to the modern low-end (and not only) applications Current $work language is Go, which is so painful to use. I often look wistfully at all of Zig's features that improve on what Go does (particularly with regard to error handling). I hope in the future I can use Zig as a Go replacement and not just a C/C++ replacement.
- oconnor663 4y ago> I hope in the future I can use Zig as a Go replacement I've only scratched the surface of Zig myself, but my impression is that replacing Go with Zig will probably be painful in most cases. I think of the stereotypical Go project as a backend API service, where memory is relatively plentiful, and "make a copy of this string" is something you do all the time without thinking twice about it. It seems like Zig wants you to be more thoughtful whenever you're allocating memory, which makes a ton of sense for low level libraries or kernel code, but which sounds painful for typical large applications.
- mirekrusin 4y agoStrings are immutable in go so they don't need to be copied? https://go.dev/play/p/o-ly05_Q46E https://go.dev/play/p/o-ly05_Q46E
- oconnor663 4y agoYes I know :) Equally importantly, the Go GC will keep a string alive as long as you have a reference to it, so different objects can hold a pointer to the same string without worrying about who's going to free it or whether one of those pointers might become dangling. Python and Go (and others?) get a lot of mileage out of making strings shared and immutable, but languages with manual or destructor-based memory management don't really have that option.
- kuon 4y agoI love zig but I have two pain points. Function naming convention as camelCase. I can overlook that and use snake_case in my code, which I do and people frown upon me. Second is inability to turn unused var check off.
- boywitharupee 4y agoFor unused vars you can do this and the compiler will ignore it. _ = my_unsed_var;
- adamdusty 4y agoDoesn't that entirely defeat the purpose of having the compiler not allow unused variables.
- jayd16 4y agoWhat is the appeal of small languages? If it's simplicity, it seems like complex programs would be supported by complex libraries instead of complex language features, leading to the same level of complexity but with less consistency. "Smallness" seems to be a sought after feature but I'm not sure why.
- dangets 4y agoAs the total number of ways to accomplish a task increases it puts more burden on a programmer to recognize the usage and understand the semantic differences between the options. How many ways are there to iterate over a collection in say C++? Libraries _can_ hide some of this, but then that might also be yet another function or operator that does the same thing. Internalizing when to use one method vs. another adds to the total cognitive load of using the language, and it takes away from the mental capacity available for the problem you are actually trying to solve. Sometimes these differences are necessary and important, other times they are less so.
- bluGill 4y agoThe number of ways to do something isn't the problem with C++, it is the number of inconsistent ways, some of which have big footguns. If the different ways were all consistent it would make it a lot easier to remember which was the right one to use in any particular context.
- naikrovek 4y agosimpler languages are easier to learn and to remember. small languages make software easier to write and to read. complex features (in a language that does the simple things correctly) are always easily composed from the simple features available. the antithesis of small languages is C++, which is probably the most popular language on the planet in which 0% of its users know 100% of. with a small language, i can’t use language features which you do not understand when it comes to read my code or take over maintenance, or even understand it.
- bluGill 4y ago
- sakex 4y agoI don't know Zig, but aren't we missing a return here? fn count_nonzero(a: []const i32) i32 { var count: i32 = 0; for (items) |value| { // "for" works only on arrays and slices, use >"while" for generic loops. if (value == 0) { continue; } count += 1; // there is no increment operator, but there are shortcuts for +=, \*=, >>= etc. } }
- anonymoushn 4y agoThat's correct.
- goldenshale 4y agoAlso shouldn't the input argument a be named items instead? From what I can see items is an invalid variable as is.
- sakex 4y agoMight be, I assumed it was one of the const-variables declared before, but didn't bother to check
- nrclark 4y agoI'm not super-great with Zig, but my understanding is that `count` should be the return value, post-increment. When the return value is unspecified, Zig will default to an implicit return of the last-calculated rvalue. The same behavior is also found in Ruby, Lisp, and some other languages as well.
- RcouF1uZ4gsC 4y agoIME, general purpose "small" languages rarely stay small. Even C has accreted a lot of features. I think Python was small at some point. Pascal was small at some point, but by Delphi had become quite big. Go has just added generics. I think looking at a "smallness" for a relatively young language is likely to be misleading.
- drcode 4y agoI mean, zig manages full generics while still being simple
- kristoff_it 4y agoZig is meant to stay small by design, just read all the angry comments about missing features in this very thread :^)
- dolmen 4y agoThe Go team did an amazing work to add generics with a very small impact on the language. They even managed to add generics without breaking the Go 1.0 compatibility guarantee. Check the short list of changes to the language specification in the Go 1.18 release notes: https://go.dev/doc/go1.18#generics https://go.dev/doc/go1.18#generics Go is still a small language, even with generics.
- Graziano_M 4y ago// Arrays may contain a sentinel value at the end, here array.len == 4 and array[4] == 0. const array = [_:0]u8 {1, 2, 3, 4}; Small typo. Len should be 5, I believe.
- ptato 4y agoIt's really 4. It doesn't count the sentinel, just the elements. `@sizeOf([4:0]u8)` is 5 though.
- Graziano_M 4y agoOh, that's strange. So you can actually read (just 1) past len?
- sl3dge78 4y agoYeah, it's made to feel like strlen in a way. Sentinel types are mostly used with C code. That way strlen(some_string) == some_string.len.
- Graziano_M 4y agoAh, I hadn't thought of that analogue, but it makes sense, then. It sort of recreates the opportunity to make the common bug, though, of allocating enough for the data but not the sentinel (or enough for the string but not the null terminator).
- kristoff_it 4y agoNot really, the sentinel is part of the type and the allocator interface in Zig is type-aware, meaning that you can't simply "forget" about sentinels. The type system will fight you.
- Graziano_M 4y agoSo creating a pointer to a type with a fixed length and a sentinel and creating a new one will automatically allocate enough for the length + the sentinel? That's nice. I'm still not sure I like the .len 'misreporting' since it would be harder to manually optimize around, e.g. packing a struct with a 8 byte + sentinel array would take 16 bytes that I might not notice if I am going by `.len`. This is all speculation, though. I need to actually play around with Zig.
- synergy20 4y agois zig still one man's work and has a hit-by-a-bus risk factor? checked it a few weeks ago for a few hours, looks good, but I don't feel I need switch from c to zig yet. will re-try after 1.0 is out.
- ptato 4y agoThe "People" section on the right on their github organization[0] shows members of the core team. It's also free software and surrounded by a passionate community, so it's pretty hard that it would die out. Andrew is irreplaceable though. [0] https://github.com/ziglang https://github.com/ziglang
- AndyKelley 4y agoLegally I am extremely replaceable because Zig Software Foundation is a 501(c)(3) non-profit, and I am only one of three board members who can make legally enforceable decisions. As a software engineering lead I am becoming more replaceable every year as others learn to maintain various components of the project. Recently, I am proud to have dropped to having fewer than 50% of total commits in the Git repository for the compiler.
- nobleach 4y agothe scarier prospect right now is that if you DO wait for 1.0, it'll likely have a very different API.
- cercatrova 4y agoWhy should I use Zig coming from Rust? It doesn't seem that Zig actually solves the memory problems that Rust does.
- ok123456 4y agoNot fighting the borrow checker or making gratuitous copies of data to satisfy the borrow checker. Zig's scope is just to be a better C that's free to add modern features like optional types, compile time expressions instead of string-macros, source level modules, packages, a more expressive syntax for writing bit-packed structures, a standard testing framework, deferred function calls, and so on. You can also directly include c headers directly in zig code, so this gives you a pathway to modernizing C Code incrementally.
- cercatrova 4y agoBut the problems still remain? The borrow checker is an automated way of what one would normally check by hand, or in their mind. Removing it means that, just as one does in C, one must still check for memory errors and will more likely miss such errors more than the borrow checker does.
- ok123456 4y agoThey have a build in arena allocator. That would cover a lot of manual frees. Also they have the defer syntax to defer your call to free. The borrow checker doesn't stop most memory errors, just the easy ones at the cost of making it painful to write basic data structures. There are many memory related CVE on common rust libraries.
- davidatbu 4y ago"The borrow checker stops only easy memory errors" is not the impression I have of Rust, and I would love to learn more about this claim. For example, I'd love to see examples of CVEs on common rust libraries that you mention, if they are caused by safe code (ie, not using `unsafe`, which means the borrow checker is in effect).
- beltsazar 4y agoWhy would anyone use a new systems programming language that is not safe? Zig might be better than C in some aspects, but is it worth it to switch to Zig when you realize that it's not much safer than C? [0] [0]: https://www.scattered-thoughts.net/writing/how-safe-is-zig/ https://www.scattered-thoughts.net/writing/how-safe-is-zig/
- rtev 4y agoIt does seem confusing to me why someone would choose Zig. If you’re looking for a low-level language, Rust is safer with a better ecosystem. If you want a high-level language, Nim binaries are smaller and the Go ecosystem is better. When is Zig the best choice?
- ferdowsi 4y agoI would choose Zig because Zig has a much more approachable learning curve and Zig code is eminently legible compared to Rust code and it's overly complex type system.
- k__ 4y agoWhy not Go or Nim?
- stephc_int13 4y agoBecause Garbage Collection is basically a garbage feature that should be, at the very least, optional.
- TEP_Kim_Il_Sung 4y agoI hope this takes off. For great justice.
- ImprobableTruth 4y agoI really wish Zig had closures and interfaces/traits. You can emulate them in hackish ways, but UX is awful and as there isn't a uniform way, there's next to no compatibility with regards to libraries. As is I'd rather just use a heavily restricted version of C++.
- bouiaw 4y agoYeah I miss interfaces/traits as well, see https://github.com/ziglang/zig/issues/1268 https://github.com/ziglang/zig/issues/1268 related open issue.
- Jarred 4y agoIf you want to build stuff using Zig full-time, Oven (https://oven.sh https://oven.sh) is hiring Zig engineers. Email jarred@oven.sh to apply
- Cyberdog 4y agoWould love to hear your experiences hiring devs and building projects in a new, scarcely-used language and ecosystem like this. I imagine you don't quite get the range of applicants you'd see hiring devs for Rust or Go or other trendy languages du jour, but maybe that's actually a good thing since it weeds out less experienced devs and résumé liars who probably don't even know Zig exists…?
- mirekrusin 4y agoCounter intuitively you'll get really good people and won't have problem hiring? At least that's what happened years ago at Jane Street with OCaml.
- hsn915 4y agohttp://paulgraham.com/pypar.html http://paulgraham.com/pypar.html Although the "Python Paradox" does not apply to Python anymore (it has become even more mainstream than Java), it does highlight an important point: If you choose your language based on "I want the biggest pool of developers" you are also saying "I want the most average and mediocre developers that I can pay for cheap".
- sarchertech 4y agoI noticed you deleted the tweet where you said employees would need to grind for the first 9 months, and that if you're looking for somewhere with a good work life balance this job isn't for you. Does that mean you've rethought that position? https://news.ycombinator.com/item?id=32584211 https://news.ycombinator.com/item?id=32584211
- ctas 4y agoAlso, if you're a company looking to hire Zig devs check out zigjobs.org. (Disclaimer: I made it.)
- skocznymroczny 4y agoI like the idea of Zig, but haven't got to actually use it yet. I still can't get over it's syntax. I think the manual memory management approach but easier than C is a good path to follow. It makes Zig a natural in other environments such as WASM. Compare that to Go or D which started with a garbage collector and struggle in WASM (Go has to compile it's garbage collector to WASM to work, D forces you to manage your memory manually).
- samoshay 4y agoAre the top three links on HN right now all hosted on the same blog template?
- joshmarinacci 4y agoIt feels like there's room for a better C today. I don't mean a really different language. I'm thinking something that is semantically C, and 99% syntactically C, but that a much better story around macros, builds, modules, linking, volatile pointers, and all of the other C foot guns. Not C++ or Rust (though I love Rust). I'm thinking just a smoothed out C. And done in a way where a codebase can incrementally move from C to better C, one file at a time.
- timbit42 4y agoWould the "Better C" in dlang work for what you want?
- markoutso 4y ago
- avgcorrection 4y agoI’m in favor of big general purpose languages. Why? Not for their own sake. Just because it’s tedious to use multiple languages. - How do they communicate? Maybe Json if it’s just data. For more control you need FFI - And FFI is such a hassle that some languages seem to focus mostly on getting a nice C FFI - ... That lingua franca that proves that you can have all the simple languages that you want as long as its name is one letter and starts with “c” - All kinds of minute differences that are real tradeoffs when it comes to each individual language become major pains when switching between them - Sometimes you seem to have to rule out languages just because they don’t have good libraries for X. Or can’t do concurrent tasks. How are multiple small languages supposed to flourish when 20+ year old languages can’t get good library coverage or get rid of their global interpreter lock? I would like small languages if they all played nicely together. But they almost never do.
- flumpcakes 4y agoZig can directly compile C and C++ code. It's probably one of the easiest languages for C/C++ interop.
- avgcorrection 4y agoOk.
- dirkg 4y agoI hadn't even heard of Zig till the recent news everywhere on dev blogs about bun.sh, and AFAIK it remains the biggest production project using it. I'm sure there are plenty of other languages such as Nim etc that never get similar exposure.
- slimsag 4y agoWe're using it to build a game engine[0] competitive in spirit with unity/unreal/godot, right now it's just a really nice way to use WebGPU natively on the desktop[1] though. [0] https://machengine.org/ https://machengine.org/ [1] https://machengine.org/gpu/ https://machengine.org/gpu/
- augustk 4y agoIf you like small languages you may also be interested in Oberon (which is smaller than Zig). https://www.miasap.se/obnc/oberon-report.html https://www.miasap.se/obnc/oberon-report.html
- 1vuio0pswjnm7 4y agoThe size of the compiler toolchain is similarly small. For example, according to xbps on Void Linux, GCC is a 172MB install. Zig is only 166MB. A Clang or Golang install is twice the size of Zig. A Rust install is three times the size of Zig. And so on.
- brundolf 4y agoIt just occurred to me, Zig could be a great learning-language for CS When I was in college we mostly used C++, which exposed (and thereby taught) a lot of core concepts around how computers and low-level languages work. But it was also a bit of a nightmare for... unrelated, obvious reasons. Rust is great for building software, but as a learning language I think it introduces too many additional concepts, has too many additional constraints, and has too many magical abstractions. Eg, doing manual deallocation helps you later appreciate what the borrow-checker does and why it exists Zig is supposed to be C++ without all the pain and misery (esp around tooling). That could make it perfect for teaching people about pointers and heap allocations and working with bytes, with minimal extra pain and friction
- gautamdivgi 4y agoI think it’s supposed to be C without all the pain and misery. C++ without pain and misery is allegedly Rust :). But we were thrown into C and UNIX when I was at university. I think I’ll have Bus Error and Segmentation Fault core dump messages etched on my gravestone and may be request that the flower vases be labeled gdb (I forget the native debugger that came with the UNIX flavor I was using). Either way we did data structures and algorithms with Pascal. But I think c should be essential learning from a systems perspective. Maybe I’m too old though.
- skohan 4y agoZig might be a good alternative as it matures. As far as I understand, zig makes a lot of things explicit which are not in C, like explicitly passing the allocator when it’s needed. This might be even more edifying than C which has a little bit of magic going on.
- tialaramex 4y agoI still think the first language when I was an undergraduate (so, last century) was the correct choice: an ML in our case SML/NJ The stuff about how this actually works can come later, we're not teaching electronics students here (or are we? Zig as first language for electronics students makes some sense) I agree that (safe) Rust has too much stuff for a first language for Computer Scientists. Lifetimes! Polymorphism! Two entire Macro systems! although it's simpler than C++, surely nobody should teach that as first language. Zig has a compelling case as a second language if you learned something less practical like ML as first language. Apparently a colleague now teaches Haskell to the equivalent undergraduate classes as a second (or maybe third?) language, and I think Rust would also fit in there for the same purpose but given how practical the current first language is (now Java) he probably doesn't want more practicality. One thing I'd say for Rust is that we need to start teaching Safety and Correctness early. There's a reason the Medics don't wait until year four to do Ethics and Consent. If we want to stop writing software which is crap we need to start teaching the next generation of CS graduates that's not OK up front, not just in some Friday afternoon class about the Therac-25 which has maybe 40% attendance.