https://ayende.com/blog/194404-A/looking-into-zig Ayende @ Rahien Ayende @ Rahien [oren] Oren Eini aka Ayende Rahien CEO of Hibernating Rhinos LTD, which develops RavenDB, a NoSQL Open Source Document Database. [mvp-horizo] [dzone-hori] Get in touch with me: [ico_mail]oren@ravendb.net [ico_phone]+972 52-548-6969 [mvp-vertic] [dzone-vert] Posts: 7,187 | Comments: 50,211 Copyright (C) Ayende Rahien 2004 - 2021 Privacy Policy Terms [ ] [SUBMIT] filter by tags archive stack view grid view * architecture (470) rss * bugs (399) rss * challanges (94) rss * community (318) rss * databases (354) rss * design (765) rss * development (499) rss * hibernating-practices (49) rss * miscellaneous (588) rss * performance (327) rss * programming (967) rss * raven (1238) rss * ravendb.net (232) rss * reviews (181) rss * uberprof (40) rss * 2021 + August (9) + July (11) + June (16) + May (4) + April (10) + March (11) + February (15) + January (14) * 2020 + December (10) + November (13) + October (15) + September (6) + August (9) + July (9) + June (17) + May (15) + April (14) + March (21) + February (16) + January (13) * 2019 + December (17) + November (14) + October (16) + September (10) + August (8) + July (16) + June (11) + May (13) + April (18) + March (12) + February (19) + January (23) * 2018 + December (15) + November (14) + October (19) + September (18) + August (23) + July (20) + June (20) + May (23) + April (15) + March (23) + February (19) + January (23) * 2017 + December (21) + November (24) + October (22) + September (21) + August (23) + July (21) + June (24) + May (21) + April (21) + March (23) + February (20) + January (23) * 2016 + December (17) + November (18) + October (22) + September (18) + August (23) + July (22) + June (17) + May (24) + April (16) + March (16) + February (21) + January (21) * 2015 + December (5) + November (10) + October (9) + September (17) + August (20) + July (17) + June (4) + May (12) + April (9) + March (8) + February (25) + January (17) * 2014 + December (22) + November (19) + October (21) + September (37) + August (24) + July (23) + June (13) + May (19) + April (24) + March (23) + February (21) + January (24) * 2013 + December (23) + November (29) + October (27) + September (26) + August (24) + July (24) + June (23) + May (25) + April (26) + March (24) + February (24) + January (21) * 2012 + December (19) + November (22) + October (27) + September (24) + August (30) + July (23) + June (25) + May (23) + April (25) + March (25) + February (28) + January (24) * 2011 + December (17) + November (14) + October (24) + September (28) + August (27) + July (30) + June (19) + May (16) + April (30) + March (23) + February (11) + January (26) * 2010 + December (29) + November (28) + October (35) + September (33) + August (44) + July (17) + June (20) + May (53) + April (29) + March (35) + February (33) + January (36) * 2009 + December (37) + November (35) + October (53) + September (60) + August (66) + July (29) + June (24) + May (52) + April (63) + March (35) + February (53) + January (50) * 2008 + December (58) + November (65) + October (46) + September (48) + August (96) + July (87) + June (45) + May (51) + April (52) + March (70) + February (43) + January (49) * 2007 + December (100) + November (52) + October (109) + September (68) + August (80) + July (56) + June (150) + May (115) + April (73) + March (124) + February (102) + January (68) * 2006 + December (95) + November (53) + October (120) + September (57) + August (88) + July (54) + June (103) + May (89) + April (84) + March (143) + February (78) + January (64) * 2005 + December (70) + November (97) + October (91) + September (61) + August (74) + July (92) + June (100) + May (53) + April (42) + March (41) + February (84) + January (31) * 2004 + December (49) + November (26) + October (26) + September (6) + April (10) RavenDB 5.2 - OLAP ETL, Telegraf & Grafana Integration, Cluster Dashboard previous post next post Aug 05 2021 Looking into Zig time to read 5 min | 939 words --------------------------------------------------------------------- I think that it was the Pragmatic Programmer that recommend that you should learn a new language a year. For me, in 2020 that was Rust. I read a bunch of books about the language, I read significant amount of code and wrote some non trivial amount of code in Rust. That was sufficient to get me to grok the language, I'm not a Rust developer by any mean, but I walked with Rusty shoes for long enough to get the feeling. This year, I decided to look into Zig. Both Zig and Rust are more or less in the same space, replacing C. They couldn't be more different, however. Zig is a small language. I spent a couple of evenings going through the language reference and that was it, I had a pretty good idea about how to do things. The learning curve is mostly flat, and that is pretty huge. This is especially because I can't help but compare Zig to Rust. I spent a lot of effort understanding Rust, but I had spent almost no cycles trying to grok Zig. It was simple, obvious and quite clear. In terms of power, mind, I would rate both languages on roughly the same spot. You can write really nice code in Zig, it is just that you don't need to bend your head into funny shapes to get the compiler to accept your code. One of the key features for Zig is its comptime feature. This is a feature that allow Zig to run code at compilation time. That isn't a new or exciting feature, to be honest, C++ had it for many years. The key difference is that Zig can use this feature for code generation. For example, to create a generic list, you'll write the following code: Note that we are writing here a function that returns a type, which you can then use. That approach is incredibly powerful, but at the same time, this is simple, it is obvious. Zig is easy to learn, because there isn't a whole lot more that is hidden from you behind the scenes. That actually leads to another really important aspect in the design of Zig. There isn't anything behind the scenes. For example, you cannot allocate memory in Zig, there is no global function that will do that for you. You need to do so using an allocator. That means that the fact that memory allocations can fail is pervasive throughout the API, standard library and your code. There isn't a " this may fail on rare occasions" scenario that you somehow need to handle, this is clear and in your face. At the same time, Zig does a lot more to make things easier than C. I want to focus on a few important points: * Zig has the concept of Errors. In the code above, the function push() may fail because of an allocation failure. In this case, the function will fail with a return code. That is handled by the try keyword, which will abort the current function and return the error. Note that errors and regular values are separate channels in Zig (there is a union mark with ! at the function declaration). * Zig has support for defer and errdefer keyword. The defer keyword works just as you would expect it to, at the function exit, it will run all the deferred statement in reverse order. The errdefer is a lot more interesting, because that will only run if the function exits with an error. This seemingly simple change has a huge impact on the code quality and the complexity that a developer need to keep in their head. * Zig has built-in testing, to the point where test is a keyword in the language. To give you some context, when I was writing C code, I literally wrote the exact same thing (manually, with macros and nastiness) in order to help me get things done. In the same manner, the fact that allocation are explicit and managed from the top (all types that needs to allocate gets the allocator from their parents) means that you get to do some really cool things with memory. It is easy to say something like "this piece of code gets 10MB of memory only" and let it run like that. It also end up creating a more robust software system, I think, so memory allocations happen aren't a rare occurrence, they happen all the time. In general, Zig feel like a lot better C, no additional mental overhead. Compared to Rust, you can get working almost immediately and the compilation speed is excellent, to the point where you don't really need to think about it. Rust makes you feel the slow compilation cost from the get go, basically, which is noticeable as your system grows bigger. Thinking about this, I actually feel that we should compare Zig to Go, because it is closer in concept to what I think Go wanted to be. In fact, looking at the most common complaints people has against Go, Zig answers them all. If you haven't noticed, I'm quite enjoying working with Zig. And as an aside, the fact that a language can implement a language server and get automatic IDE support is freaking amazing. You can also debug Zig code inside VS Code, for example, pretty much with no more issues than you would for native code. Zig is implemented on top of LLVM and gains a lot of the benefits from it. One thing that kept going through my mind when I looked at all that I got out of the package is: standing on the shoulders of giants. Tweet Share Share 6 comments Tags: * ravendb.net * hibernating-practices * design * development * architecture --------------------------------------------------------------------- previous post next post --------------------------------------------------------------------- Comments Milosz 06 Aug 2021 03:18 AM Milosz Thanks a lot for this review. 1) wouldn't it be fair to point out that complexity in Rust comes from its memory leak prevention design and its functional style? 2) perhaps it would be cool to see you reviewing Odin language in the future? Oren Eini 06 Aug 2021 06:28 AM Oren Eini Odin looks interesting, but after Zig, I'm missing errdefer, and that is just in the few minutes I looked at. Rafal 06 Aug 2021 07:10 AM Rafal It's been quite a few years since i tried any new programming language but just by reading this i felt the same kind of joy (of discovering something unexpectedly brilliant). Or maybe it's age related that i'm enjoying reading about doing things instead of actually doing them :) Milosz 06 Aug 2021 07:20 AM Milosz The lack of errdefer may have something do to with the general approach of error handling that Odin's author have taken: https:// www.gingerbill.org/article/2019/05/13/a-reply-to-the-road-to-zig/# error-handling. It would be interesting to hear your opinion on that. Oren Eini 06 Aug 2021 08:41 AM Oren Eini Milosz, That is a good point, and a good read. I would very strongly disagree with him. See my full reply here: https://ayende.com/blog/194466-A/looking-into-odin-and-zig-my-notes? key=42d23515614840bead91eca65823b9d5 Milosz 06 Aug 2021 09:20 AM Milosz Thanks a lot for that reply and thanks for mentioning Go :) Comment preview [avatar] Join the conversation... Name [ ] Email [ ] Url [ ] Comments [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] Human? [Comment Preview] [Post Comment] Markdown formatting ESC to close x Markdown turns plain text formatting into fancy HTML formatting. Phrase Emphasis *italic* **bold** _italic_ __bold__ Links Inline: An [example](http://url.com/ "Title") Reference-style labels (titles are optional): An [example][id]. Then, anywhere else in the doc, define the link: [id]: http://example.com/ "Title" Images Inline (titles are optional): ![alt text](/path/img.jpg "Title") Reference-style: ![alt text][id] [id]: /url/to/img.jpg "Title" Headers Setext-style: Header 1 ======== Header 2 -------- atx-style (closing #'s are optional): # Header 1 # ## Header 2 ## ###### Header 6 Lists Ordered, without paragraphs: 1. Foo 2. Bar Unordered, with paragraphs: * A list item. With multiple paragraphs. * Bar You can nest them: * Abacus * answer * Bubbles 1. bunk 2. bupkis * BELITTLER 3. burper * Cunning Blockquotes > Email-style angle brackets > are used for blockquotes. > > And, they can be nested. > #### Headers in blockquotes > > * You can quote a list. > * Etc. Horizontal Rules Three or more dashes or asterisks: --- * * * - - - - Manual Line Breaks End a line with two or more spaces: Roses are red, Violets are blue. Fenced Code Blocks Code blocks delimited by 3 or more backticks or tildas: ``` This is a preformatted code block ``` Header IDs Set the id of headings with {#} at end of heading line: ## My Heading {#myheading} Tables Fruit |Color ---------|---------- Apples |Red Pears |Green Bananas |Yellow Definition Lists Term 1 : Definition 1 Term 2 : Definition 2 Footnotes Body text with a footnote [^1] [^1]: Footnote text here Abbreviations MDD <- will have title *[MDD]: MarkdownDeep Close FUTURE POSTS 1. Architecture on the cloud: Avoid writing the code - 3 days from now 2. Transaction hierarchy in RavenDB or, the value of a write - 4 days from now 3. Recent podcasts & videos - 5 days from now 4. Looking into Odin and Zig: My notes - 6 days from now There are posts all the way to Aug 12, 2021 RECENT SERIES 1. RavenDB 5.2 (2): 06 Aug 2021 - Simplifying atomic cluster wide transactions 2. Postmortem (2): 23 Jul 2021 - Accidentally quadratic indexing output 3. re (28): 23 Jun 2021 - The performance regression odyssey 4. Challenge (58): 16 Jun 2021 - Detecting livelihood in a distributed cluster 5. Webinar (4): 11 Jun 2021 - Machine Learning and Time Series in RavenDB with Live Examples View all series RECENT COMMENTS * Yeah, true, forgot about the single node case in 3.x .... But let's rephrase the question then: how to protect the documen... By Ryan Heath on RavenDB 5.2: Simplifying atomic cluster wide transactions * Ryan, In 3.x optimistic concurrency would only be enough if you are using a single node (same holds in 5x)The question is ... By Oren Eini on RavenDB 5.2: Simplifying atomic cluster wide transactions * Yes, I see that scenario, but what in case we have a single document that may be altered by different users at the same time.... By Ryan Heath on RavenDB 5.2: Simplifying atomic cluster wide transactions * Ryan, The idea is that there are two levels of updates here. In the first level, you want to reserve a unique name for the u... By Oren Eini on RavenDB 5.2: Simplifying atomic cluster wide transactions * "A common scenario for this code above is that you'll include the usernames/ayende reservation document whenever you modify t... By Ryan Heath on RavenDB 5.2: Simplifying atomic cluster wide transactions Syndication Main feed Feed Stats Comments feed Comments Feed Stats