20 ms·
Some Thoughts on Zig
- deleted 4y ago[deleted]
- deleted 4y ago[deleted]
- socialdemocrat 4y agoNice balanced writeup! However, I am not convinced one can make something solving all the things Rust solves which is substantially simpler as language. Whether we look at Zig, Go or Julia they stay relatively simple because they simply avoid solving certain problems. I think that is a fair tradeoff. I think good language design is really about figuring out what problems can we dump on the user which they will be able to handle relatively well. A reason why there are so many languages I think is because different people have different preferences in terms of what problems the want to avoid dealing with and what problems they are fine dealing with. Personally I think that if you "solve" a problem by making the language so complex that developers cannot easily read and reason about the code, then you haven't actually solved any problem. Priority must always be placed on the ability of developers to read and understand code. Unreadable code will naturally be a source of bugs.
- vageli 4y ago> Whether we look at Zig, Go or Julia they stay relatively simple because they simply avoid solving certain problems. Could you expand on the specific problems that each language avoids?
- sk0g 4y agoTill recently, Go lacked generics. Aside from that, it has a GC that is pretty nice to work with. Manual memory management is semi-supported, using C interop instead of native language constructs (from when I last looked into it).
- kaba0 4y agoGo is a thoroughly uninteresting language bringing nothing new to the table. D, C#, even Haskell all has and had for decades now GC with manual memory at places. Other than virtual threads, which I have to give credit for, there is zero new thing from a PL perspective.
- flohofwoe 4y agoErm, Go channels were a pretty big deal in the beginning, as well as the idea of creating a 'better C' (instead of a 'better C++').
- kaba0 4y agoI absolutely don’t see anything in Go resembling C from a technical perspective, other than (before generics) being similarly unexpressive. It is much closer to JS than C in any way and shape.
- flohofwoe 4y agoI don't know, I currently write both C and JS on a daily basis, and I just can't see how Go is anything like JS. For me, Go is definitely in the C syntax family, while trying to solve roughly the same problems where Java has been used extensively (backend services).
- masklinn 4y ago> Erm, Go channels were a pretty big deal in the beginning In what world did people not know about MPMC queues?
- flohofwoe 4y agoWhen Go came around I wasn't aware of other popular languages which has channels as a core language feature (but maybe I was just ignorant).
- adgjlsfhk1 4y agoJulia mostly ignores static compilation (although there now is work to add the ability to statically compile for a relatively large but limited subset of the language). The big thing that this allows is for you take the ideas of C++ templates to the extreme. In Julia pretty much every function behaves like a C++ function that is templated on the type of every input. This has the major advantage that it is really easy to write extremely fast generic code. This only works in general though because Julia only has to generate code for the types that actually appear rather than all the type combinations that static analysis can conclude might appear.
- agentultra 4y agoUnreadable as in, “this code is unfamiliar and I haven’t talent the time to understand it,” or “this code is syntactically designed to be difficult to parse by humans and I cannot read it?”
- abirch 4y agoWhat about, I can't read this code. Who's the idiot who wrote it? git blame Oh that was me.
- timeon 4y agoThis was often my issue. But I remember while learning more Rust I was like: 'now I can go back to my early Rust project to improve it!' and when I did check it: 'oh.. this is actually not bad! How was I able to do it?'
- verdagon 4y ago> Nice balanced writeup! However, I am not convinced one can make something solving all the things Rust solves which is substantially simpler as language. I used to think this too, but my opinions have evolved a bit. Recall the transition from goto-heavy assembly code to C; it solved all the things assembly solved, and was a substantially simpler language. One of the keys to designing a good language (or any good abstraction, really) is to identify the patterns that already informally exist. So if we want to see a simpler Rust, we need to look at the informal patterns that exist in Rust. * Zig did this with Rust's async/await, by making it colorblind. [0] * Vale did this with Rust's generational indices, by making generational references, a way to get memory-safe single ownership without a borrow checker. [1] It then adds a hardened FFI boundary to get more solid safety guarantees than Rust could. [2] * HVM did this for Haskell by looking at Rust's particular blend of borrowing and cloning, and making it happen automatically. [3] * Lobster did something similar for an imperative mutable language, to reduce RC costs lower than ever before. [4] I think Rust is an amazing first attempt at an interesting new problem space, and a huge step forward for the field. Now, language designers are starting to notice that the building blocks that Rust trailblazed might be combined in new interesting ways, to make new interesting languages. It's an exciting time for programming languages! [0] https://kristoff.it/blog/zig-colorblind-async-await/ https://kristoff.it/blog/zig-colorblind-async-await/ [1] https://verdagon.dev/blog/generational-references https://verdagon.dev/blog/generational-references [2] https://vale.dev/memory-safe https://vale.dev/memory-safe [3] https://github.com/Kindelia/HVM https://github.com/Kindelia/HVM [4] https://www.strlen.com/lobster/ https://www.strlen.com/lobster/
- nyanpasu64 4y agoSaying C solved all the problems assembly solved is a bit too strong; bootloaders and kernels, green thread or coroutine implementations (like libco), and cryptographic code are all implemented in part in assembly.
- flohofwoe 4y agoIMHO if C could solve all the same problems as assembly, it would need to be down on the assembly language level - which it isn't, C is clearly a high level language, it's much closer to any other programming language than any assembly dialect (maybe with the exception of Forth-like languages). In general, the higher level the language, the more flexibility you need to give up (but that's ok for many solutions).
- josephg 4y ago> However, I am not convinced one can make something solving all the things Rust solves which is substantially simpler as language. I wonder a lot about this too. As far as I know, rust is alone. It’s the first language to exist which does compile time memory management in a safe way. Every other language either is memory-unsafe (C, Zig) or garbage collected. (Go, JS, etc). Rust might the first language of its kind, but it probably won’t be the last. I’d be very surprised if we’re still using rust 100 years from now. Subsequent languages can learn from rust’s accidental complexity and clean up the approach. Personally I suspect it’s possible to have a borrow checked language that’s much simpler than rust. I don’t know what that would look like, but it would be surprising to me if we found the best programming language in this space on our (collective) first attempt at it. But I don’t have enough insight to figure out what rust’s successor would look like either. But luckily for both of us, there are a lot of smart people thinking about the problem now. Let’s give it a decade and see.
- pcwalton 4y agoThere have been memory-safe non-GC languages before, like Ada SPARK, but they generally disallow heap allocation altogether. If you heap allocate you get a GC. There were also some academic languages, most notably ML Kit, that offered a safe no-GC region-based memory management system that has a lot in common with Rust. Also, there are academic languages like Cyclone that have a very Rust-like memory management system, but they all use GCs in the end. Still, if you know Rust, Cyclone's ideas will be very familiar.
- throwaway17_17 4y agoSmall point of contention, but it’s actually Rust that has a Cyclone-like memory management system. Cyclone’s initial release was ‘02 and it finalized in ‘06.
- pcwalton 4y agoOh, I'm sorry, I didn't mean to imply that Rust invented any of those things. Cyclone was a big influence on Rust actually. We chatted with Dan Grossman early on, and his presentation "Existential Types and Imperative Languages" is the earliest description of the problem the borrow checker is trying to solve (check it out, it's a great presentation!) We stand on the shoulders of giants :)
- Nadya 4y agoI'm keeping an eye on Vale [0] but will continue to use Rust for hobby projects because the community/tooling that has built around it has made it much easier over a few years than when I first gave it a try in 2018 and especially as support for development on Windows grew. I went from absolutely struggling to get so much as a win32 GUI window to open [1] to writing a basic counting app [2] for a game I play to completely rewriting that app to not suck [3] (even though the code is still awful). Zig looks nice to read and understand. However at my skill level (more or less a skript kiddie) I don't trust myself to not cause major memory-related bugs. In fact, I can't help but wonder if it's hubris or what when extremely intelligent, college educated, programmers with more than my lifetime of experience who still manage to write programs riddled with memory safety issues and in the very next breathe say memory safety isn't an issue just write safe code. I wonder what skill/knowledge thresholds are where "I don't trust myself to not make those mistakes" turns into "I won't make those mistakes."? Honestly it reminds me of the low IQ/high IQ meme [4]. [0] https://vale.dev/ https://vale.dev/ [1] https://gitlab.com/NadyaNayme/rusty-eyes https://gitlab.com/NadyaNayme/rusty-eyes [2] https://github.com/NadyaNayme/Dorothy https://github.com/NadyaNayme/Dorothy [3] https://github.com/NadyaNayme/Dorothy-egui https://github.com/NadyaNayme/Dorothy-egui [4] https://kimiwo.aishitei.ru/i/sZaEWnY3NvfXecLm.png https://kimiwo.aishitei.ru/i/sZaEWnY3NvfXecLm.png
- kaba0 4y agoI have to agree. Adding safe, non-automatic memory management to a language will increase its complexity considerably, and I don’t see it changing even with the below mentioned new languages/techniques. Even just fundamentally, manual memory management begets functions that know about the memory architecture of your program, solidifying a given architecture. Changing this latter will result in a recursive refactor, which even if safe, is problematic/complex. Sure, it is a worthy tradeoff in certain, very niche cases (though other than writing other performant runtimes and realtime audio processing, I fail to see another area where a GC couldn’t fit - including even OSs), for which we need a language, but I really don’t get this recent hate for garbage collection, when it is truly one of the biggest productivity improvements for a PL, while simultaneously it being very performant (quite likely outperforming any naive manual take on more complex problems).
- gavinhoward 4y agoI think you are absolutely right about everything you mentioned, except for one: > I am not convinced one can make something solving all the things Rust solves which is substantially simpler as language. You could be right about this, but boy, I hope you are wrong. And I'm trying to prove that it's possible. I think the answer is structured concurrency [1], and here's why: 1. Structured concurrency can replace everything async/await can do. At least, I believe so, kind of like it's believed that structured programming can replace any unstructured use of goto. 2. If you extend structured concurrency a bit so that the thread tree is actually a DAG [2], you basically get a borrow checker for free because every item is rooted in a thread stack somewhere, and where it is rooted is always before everything that it could own and after everything that could own it. 3. Making the thread tree a DAG also means it's easy to implement RAII and have it work meaning no use-after-free, no double frees, etc. Put that on top of a language that has memory safety by default, and you will have everything that Rust gives you without the complexity. This isn't theoretical; I have implemented this in C. [3] (See [4] for a use of it.) I have even implemented bounds checking in C. [5] However, I'm also implementing a programming language with these ideas (see [6] for the example file and [7] within that file for how structured concurrency might be exposed in the language). As for readability, I do think that what I have is readable. However, I appreciate comments; readability is important to me too, but I've spent so long with this work that I am blind to what is actually readable to other people. [1]: https://gavinhoward.com/2019/12/structured-concurrency-definition/ https://gavinhoward.com/2019/12/structured-concurrency-defin... [2]: https://lobste.rs/s/8msejg/notes_on_structured_concurrency_go#c_wkxbvb https://lobste.rs/s/8msejg/notes_on_structured_concurrency_g... [3]: https://git.yzena.com/Yzena/Yc/src/branch/master/include/yc/threadset.h#L104-L115 https://git.yzena.com/Yzena/Yc/src/branch/master/include/yc/... [4]: https://git.yzena.com/Yzena/Yc/src/branch/master/src/rig/build.c#L972-L1020 https://git.yzena.com/Yzena/Yc/src/branch/master/src/rig/bui... [5]: https://git.yzena.com/Yzena/Yc/src/branch/master/include/yc/array.h#L208-L263 https://git.yzena.com/Yzena/Yc/src/branch/master/include/yc/... [6]: https://git.yzena.com/Yzena/Yc/src/branch/master/src/yao/example.y https://git.yzena.com/Yzena/Yc/src/branch/master/src/yao/exa... [7]: https://git.yzena.com/Yzena/Yc/src/branch/master/src/yao/example.y#L757-L765 https://git.yzena.com/Yzena/Yc/src/branch/master/src/yao/exa...
- Tozen 4y ago> ...Priority must always be placed on the ability of developers to read and understand code. The importance of this can't be overlooked, which is debatably why people and companies are constantly looking for a way to escape heavily complex languages and syntax. It is part of the thinking behind somewhat newer languages like Go, or newer offshoots like Vlang (https://vlang.io/ https://vlang.io/), which has a focus on high readability and general usability. If a language is being geared towards the majority or mass appeal, then it has to find that correct sweet spot. There is simply going to be a cap on how far complexity will be embraced or how much specialization will be found useful. “Genius is making complex ideas simple, not making simple ideas complex.” — Albert Einstein > ...I am not convinced one can make something solving all the things Rust solves which is substantially simpler as language. Debatably, such languages making the attempt, are less likely to succeed. Off rip, Rust already occupies that space. So, trying to make a "better Rust", which is already a specialist in that field (safety) and has already got "the push" from large corporate backers (Mozilla, Samsung, Quantum...) will usually mean failure, remaining in niche status, or overcoming incredible odds (like hitting a Lotto jackpot). The more the language specializes on that particular use case, then arguably the less general appeal and general use. And if there is already competition doing it better or being more known for that focus, the harder the hill to climb. For that matter, trying to make a "better C", is another tough hill. Not only because C is so entrenched and established, but that there is already a long list of quasi "better Cs" out there such as Object Pascal, D, Nim, etc... Add in more complex or confusing syntax, less features, less functionality, or less usefulness... Then the hill gets even more steep. Hitting the bullseye, just isn't easy.
- jjtheblunt 4y agoHow much, of what is exposed as the complexity of a language, could be realized in the form of sequences of optimizing compiler passes, perhaps automatic or perhaps flag-driven, such that the programmer user experience remains clean-ish yet the sophistication of generated code is on par with a language like Rust? I don't know the answer, but I find it a curious question. (And are SBCL internal compilation optimizations an example of this idea?)
- ekidd 4y ago> How much, of what is exposed as the complexity of a language, could be realized in the form of sequences of optimizing compiler passes, perhaps automatic or perhaps flag-driven, such that the programmer user experience remains clean-ish yet the sophistication of generated code is on par with a language like Rust? This is an excellent question, and one the Dylan designers wrestled with in the 90s. How could they make a high-level language with many of the best features of Lisp, while still making it possible to perform like C? The strategy they chose was: 1. Offer powerful features with simple interfaces, even though performance was poor in some cases. 2. Provide "fast path" versions of these features that handled common cases with high efficiency. This turned out to be something of a disaster. Compiler implementors needed to implement the slow path, plus code to detect when to use the fast path, and finally the fast path itself. This ended up being 3x the work of just writing the fast path. And to make things worse, users never really knew what constructs would cause them to hit the slow path. (And yes, this is similar to the challenges SBCL faces, but SBCL has had a lot more time to work on them.) Rust has two major sources of complexity: (a) the ownership model makes certain common architectures much harder to write (particularly with GUIs and non-ECS games), and (b) Rust cares about references and memory layout and lots of system-level stuff like that. The latter is hard to abstract away without losing performance. I love Rust, and I look forward to seeing a new generation of languages remix its ideas. But I don't think that pairing a default-inefficient language with a "sufficiently smart compiler" is the best way to get there.
- LAC-Tech 4y agoI am all in on Zig. I was more producitve with it in a few weeks than I was after a few months of rust. Yes the memory management is "manual" but the std library functions for dealing with it are top notch, you have to free a lot less due to the ease of using arena allocators, and the testing allocator does a valgrind-esque runtime analysis of your code - very quickly and very thoroughly. In practice, it's not an issue, and unlike with rust I have a very clear idea of when and where memory is being freed and allocated. That having been said - the core team chooses some incredibly dumb hills to die on. Last time I checked the tab character was illegal in Zig code - though I understand that's changing. More concerning is that un-named parameters are a compiler error and this cannot be adjusted. Meaning that if you want to test on save, you can't if any of your parameters are unused. Enforcing such a thing at the compiler level because of your personal coding style is so mind-numbingly short sighted. The guy who made it is clearly a very smart and insightful, but I acknowledge you probably have to be fairly stubborn to decide you're going to replace C.
- pcwalton 4y ago> In practice, it's not an issue, and unlike with rust I have a very clear idea of when and where memory is being freed and allocated. I'm very skeptical that the lack of memory safety is not going to be an issue "in practice". That's what every C++ developer thinks too until they write code people actually try to attack. One of the very nice things about memory safety is that it frees the programmer from having to think about when memory is freed most of the time. The compiler or runtime system will take care of that. Most of the time, manual frees are just noise--this observation is the entire reason behind destructors.
- LAC-Tech 4y agoLearn Zig and get back to me. It's nothing at all like managing memory in C++, not even close.
- pcwalton 4y agoWe've had close to 50 years to answer the question "is memory safety necessary to write secure code under normal industry development practices"? During this time dozens of languages have come and gone, and the evidence still overwhelmingly points to "yes". It's going to take a lot to make a convincing argument otherwise, especially since Zig doesn't really do much different from C here, semantically speaking.
- Schroedingersat 4y agoI think this is missing one major difference in the goals of Zig vs. something like rust. Zig wants to make it possible (and as easy as is practical without ballooning the language) to make a 100% correct program. That is no panics, no UB, no failed allocation, 100% deterministic behavior. Whereas Rust wants to make 70% or so of bugs that exist elsewhere impossible.
- chriskrycho 4y agoThat’s a good goal! A 100% correct program which cannot guarantee a lack of memory safety bugs is not a 100% correct program, though, right?
- suremarc 4y agoThe absence of a proof of correctness is not a proof of incorrectness, strictly speaking.
- Schroedingersat 4y agoRust still needs unsafe blocks for full generality. Additionally there are many sources of panics that are unavoidable. The memory safety guarantees it makes are also not all bugs. It is equally possible to write a memory safe program in both (although far easier in rust), but impossible to make a panic-safe program in rust without avoiding a large portion of the language. A guarantee of the absence of certain classes of memory bugs is of no use to creating a 100% correct program if it also comes with a guarantee of panic on allocation failure and standard libraries that are full of panics. They are for different things. Rust is for keeping you completely safe from the worst things while you build something big like a web browser where the program doing something you didn't want is the worst failure state, and crashing is a mere inconvenience. It is there to replace C++ Zig is for making it possible to make yourself safe from all bugs if you try hard enough. It is for making small things that you understand fully where crashing is as bad or worse than memory errors like the firmware for a space probe. With the addition of some static analysis tools it could maybe replace or be useful in addition to something like Ada one day, and definitely replace C and some hand coded assembly.
- rektide 4y agoIt sounds like aggitation excerpted so, but the write up does a wonderful job setting up for it, making this stunner of a line not seem so bad: > For many C developers looking to upgrade their language, Rust looks a lot like a better C++, not a better C. What a line! The whats-included/what's-not takes me back to one of our legendary/epic (and at 9000 words, epic in length) posts, one I love to cite, Larry Wall's Perl, the first postmoderm computimg language[1]. A cut up early & favorite section: > When I started designing Perl, I explicitly set out to deconstruct all the computer languages I knew and recombine or reconstruct them in a different way, because there were many things I liked about other languages, and many things I disliked. I lovingly reused features from many languages... To the extent that Perl rules rather than sucks, it's because the various features of these languages ruled rather than sucked. > But note something important here. I left behind more than I took. A lot more. In modern terms, there was a lot of stuff that sucked. Now, on the feature set issue, Perl is always getting a lot of bad press... I think people who give bad press to Perl's feature set should have more angst about their reporting. > I picked the feature set of Perl because I thought they were cool features. I left the other ones behind because I thought they sucked. It's silly & basic plainly said like this, but the post goes on & on about finding creative use & also at length about deciding to leave things behind. Walls postmodernism is infectious. And this element of pruning, pairing down, is one that seems like an axis where elegance & grace can often take root & grow in todays busy, complicated world where so very much has accrued & accreted. [1] http://www.wall.org/~larry/pm.html http://www.wall.org/~larry/pm.html
- agentultra 4y agoAnother thing which makes Zig interesting is it’s approach to allocation, making a failure to allocate memory a catchable error, and enabling developers to easily assign their own allocators.
- pjmlp 4y agoTurbo Pascal 6.0 manual from 1990, page 210 http://www.bitsavers.org/pdf/borland/turbo_pascal/Turbo_Pascal_Version_6.0_Programmers_Guide.pdf http://www.bitsavers.org/pdf/borland/turbo_pascal/Turbo_Pasc... What is old is new again. :)
- Tozen 4y ago> What is old is new again. Truth!
- kaba0 4y agoHow can it guarantee that the OS doesn’t just lie about having allocated that space? As far as I know they are unallocated until a write to a given page (by default on Linux at least), and thus any write could potentially fault. Unless it zeros every page it allocated, I don’t see it being able to defend against it in practice.
- alphazino 4y agoYes, but I think you missed the point of the parent comment. > enabling developers to easily assign their own allocators Through a combination of Zig's convention to pass around allocators instead of using a global allocator, the allocator interface's ability to return errors, and the Zig language forcing you to handle all errors (unless you explicitly discard them), a lot of Zig code is written with allocation failure in mind. This means that you can seamlessly pass a custom allocator, say a bump allocator backed by a fixed buffer, to a function in some third-party library and still handle the case where the buffer runs out of space.
- zozbot234 4y ago
- ArrayBoundCheck 4y agoI never tried zig but I want to address something the article wrote > People struggle with Rust for a number of reasons ... the borrow checker! The borrow checker was the easiest thing for me to learn and the only thing I liked about rust. Except it's broken. I have to constantly reborrow after calling a constant function. The reason I actually bailed on rust is because it's a worse C++ (compiles slower, executes slower, isn't compatible with C's __thread etc etc), it's a worse webserver (I still use PHP and C#), it's worse for database heavy code, it's not very good for one offs (C# and python are better), it basically cost too much to switch for the broken borrow checker Also the article at the end doesn't make it clear what he thinks. Usually I read the start and end to see if I'm interested in the topic and I'm not sure if this article is in secret a rant
- swsieber 4y agoThe borrow checker has been through a couple revisions now, and it's got better each time.
- ArrayBoundCheck 4y agoI tried it again in 2021 and it still failed to deal with constant functions. Here's it again in 2022 https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=e5bcaac465031fdc12323b2e4a8da58a https://play.rust-lang.org/?version=stable&mode=debug&editio...
- magicalhippo 4y agoI don't know any rust but the error description[1] only talks about borrowing mutable from an immutable, which to me seems like an obvious error. However the compiler complains about the reverse condition. Again, I don't know any rust, so it's not clear why that's an error. So is it a documentation bug or a compiler bug? [1]: https://doc.rust-lang.org/stable/error-index.html#E0502 https://doc.rust-lang.org/stable/error-index.html#E0502
- 4y ago
- vitiral 4y agoGood writeup. Also I enjoyed your podcast back in the day! My current thinking is that if you want to make a "small" language with anywhere near the guarantees of rust you need to make the features smaller too. A good start is cooperative multitasking, aka basically the async model, to radically simplify concurrency. Communication between processes must then be done with channels. Another is memory allocators, I think arenas are a good start, and I'll have to look deeper into Zigs approach.
- chriskrycho 4y agoI think one of the possibly-good moves Zig makes, similarly, is using `comptime` for generic types. I don’t have a good sense of how that interacts with parametricity and ability to constrain those (and I suspect that’s an important question!) but it’s an interesting example of a possible trade off that lets the language size space. (And: thanks!)
- skribanto 4y agoRather than traits like what Rust has, the approaches I've generally seen for polymorphic behavior are explicit "interface" structs that hold a vtable, or comptime "duck-typing". I'm still rather new to Zig so there may be other approaches, but this is what I have seen so far. One positive side effect is that there is only one "language" to learn as opposed to two different syntaxes like Rust does. While most people only use a very constrained part of the Rust type system, at the end of the day it is Turing Complete[1] (or possibly close to it? I don't know enough theory to know the rigor of what constitutes Turing completeness) and trying to keep all of it in mind can be taxing.[2] [1]: https://sdleffler.github.io/RustTypeSystemTuringComplete/ https://sdleffler.github.io/RustTypeSystemTuringComplete/ [2]: https://hirrolot.github.io/posts/why-static-languages-suffer-from-complexity https://hirrolot.github.io/posts/why-static-languages-suffer...
- anonymoushn 4y agoI'm not sure exactly what is meant here, but you just have the whole language available and you can express whatever constraints you want on the types of things.
- bmitc 4y agoI don't know much about Zig, but it shares a problem with Rust in that neither have a built-in REPL. I'd like to learn Rust, but every time I try to, I am a bit overwhelmed by its complexity, both in syntax and overall feature set.
- throwaway17_17 4y agoDo you mean you want an interactive environment to just write small code samples while learning? Or do you want to do your development after learning in a REPL? I think those are two very different goals. As to Zig and Rust’s lack of REPL, they are both ahead of time compiled to executable languages. It is not super common for REPLs to be developed with any real frequency or faithfulness. (this is a little different in the functional world for some reason, Haskell, ML, etc have very thorough REPLs, and Koka’s REPL is great).
- lolinder 4y agoREPLs make a lot of sense in the functional world where everything is an expression. These languages tend to encourage writing lots of small, referentially transparent functions, which are easy to write and test in a REPL. Because the referentially transparent, when you're happy with them, you can copy what you wrote directly into your code. I think you don't see it used as much in the imperative world because each line of code tends to depend a lot on context that isn't immediately available. When you make a mistake in the REPL, you probably altered state, which means you have to run everything again from scratch to know if your corrected flow actually works. If you're going to run everything again anyway, you may as well just be working in a file with a proper text editor and a good test framework. (An exception to this might be Ruby, whose Smalltalk heritage gives it a good interactive programming legacy to build on.)
- Tozen 4y agoYet strangely Vlang has had REPL since at least back from 2020 (https://youtu.be/Pvbv4b9TibA https://youtu.be/Pvbv4b9TibA), a year after the language was released. Don't think it's as impossible as it is being made out to be here. Just like comparatively, Vlang had a self-hosted compiler the first year it came out, but Zig did not achieve this until this year (which is around 6 years after its release).
- jillesvangurp 4y agoI work with garbage collected languages all the time. Do you know how much time I spend tuning garbage collection and worrying about its performance? None whatsoever. Honestly, it's not a thing and it's mostly fine. GC mostly just works really well. Occasionally you get some poor performance and then you find that you are doing something silly and then you fix it and move on. Or you throw more hardware at the problem. GC has a bad reputation but the reality is that there is a lot of software being written with languages that aren't exactly famous for even having a particularly good garbage collector. Python, ruby, go, etc. They are alright but certainly nowhere near best in class when it comes to garbage collection. If that matters to you, you can pick python and ruby implementations that have way better performance than the default implementations provide. But the thing is, it does not matter. It's genuinely not a problem for people using those languages. I\ve used each of those languages and also use Kotlin both on the JVM and in the browser. So, you'd think I spend a lot of time obsessing about what the javascript garbage collector does in the browser or how to tune the jvm? Nope. I just let it do its thing. We have no garbage collect related issues. So why spend time on it? My observation is that engineers love talking about optimization but rarely realize how expensive they are relative to just throwing more hardware at a problem. And the thing is, some modest hardware goes a long way these days. We run our Spring/Kotlin JVM servers on cheap vms in Google cloud and in another data center using openstack. 2GB and one VCPU. About 25$ per month. That's around 10 minutes of my time at my freelance rate. Those are European rates, not Silicon Valley rates. Those are more like 3-5 minutes. We could go cheaper and I'm pretty sure I can make it run with half an GB if I work at the problem. But the savings are negligible and I like a little safety margin. We have many issues but our hosting bills are pretty awesome. Cutting them in half is literally not worth more than 1-2 hours of my time per month. I think it's great that C/C++ are getting some competition from modern languages now. Rust has a pretty innovative borrowing mechanism and it seems to genuinely be better at certain things that have been the exclusive domain for heavily optimized C/C++ systems for decades. That's great. Zig seems like the more reasonable choice for people that just want something simple that works that isn't ancient and awkward like C but gets you ballpark in the same league in terms of performance. I learned C a long time ago and I love Linux. But the never ending exploits and security problems are a thing that I just don't need in my own projects. Having a garbage collector and some other safety has a tiny cost but eliminates whole classes of bugs and exploits. Completely worth it IMHO.
- kuon 4y agoI am working with zig for embed code and it works well. It's mental model is easier than rust and it's easy to use static memory with std lib. Also I had a project where I could not get cross compilation to work and compiling on the system itself was too slow in rust. As I said earlier, both language can thrive and they both have a place in my toolbox.
- SV_BubbleTime 4y agoHow is it looking on embedded? The real make or break for me is debugging / GDB. I’ve assumed that a debugger will be able to point to and step around native Zig code, now or eventually. But I wonder how it would work with C code you bring in? If that could be debugged seamlessly along with Zig code, that would really be something.
- flohofwoe 4y agoCan't speak specifically for the embedded use case, but when debugging my mixed Zig/C/C++ projects in VSCode with CodeLLDB or the MS C/C++ extensions (which are frontends for gdc and lldb on Mac and Linux) I can just step from Zig code into C code and back (the same works for other compiled languages that support DWARF debug output btw, there are no 'language barriers' in the debugger).
- SV_BubbleTime 4y agoWell that sounds like the dream! I haven’t used CodeLLDB, but if it’s GDB access and display is as good as what I get with STM32CubeIDE (Eclipse + CDT) then it really sounds like it’s getting there.
- kuon 4y agoIt works but there are still some progress to be made, especially on low memory chip when some vars gets optimized out and you cannot inspect them without GDB jutsu involving manual address. Navigating source alongside C works fine.
- dleslie 4y agoI'm a decades-long C hacker and I'm not sure why I should use Zig anywhere that I use C. What's the killer feature/reason?
- verdagon 4y agoA few big benefits off the top of my head: * A concurrency mechanism that's very easy to use, because it doesn't have the function coloring problem of async/await. * Extra assistance with memory safety, via its "release-safe" mode. Not a silver bullet, but it does help! * Good tooling; zig cc can cross-compile from any major OS to any major OS. * Comptime, a pretty brilliant system that does what generics does for other languages, with a fraction of the complexity.
- dleslie 4y agoI've never had a problem with concurrency in C, really. async/await in other language is fine and all, and I've had my fair share of them there, but I wonder if the difficulty in reasoning about them is worthwhile wherever I use C. I'm more likely to want something declarative and explicit than async/await. Again, memory safety has never really been a problem for me where I use C; and if I want it, I'd want something as strong as Rust's borrow checker or at least the smart pointers of C++. zig cc can't cross-compile on all platforms, particularly not esoteric and proprietary embedded targets or game hardware, and so that's a nothingburger for me.
- throwawaymaths 4y agoZigs async/await is not really a concurrency construct (though you can use it as the basis for a work stealing scheduler), it's something much more fundamental and close to the metal.
- ArrayBoundCheck 4y agoFrom what I hear 1. No headers (you can use headers to import C functions. None needed for other zig functions). 2. defer 3. Comptime
- eyelidlessness 4y agoI’ve only dabbled in Rust, I’ve read about Zig with earnest interest but no bandwidth to even dabble. My primary experience is with higher level languages. It’s quite possible my intuition here is very wrong, but I’m sharing it because my experience with Rust’s lifetimes felt so awkwardly unnecessary to me. Lifetimes for memory make sense if you might modify memory. What if you don’t? My sense of a “smaller Rust” from my time exploring it was “why does everything assume it has a longer lifetime if I’m never going to access it again, if I’m never going to grant access to any other function, as soon as my function exits?” I’m coming from a functional programming perspective, which I thought I’d found when I started with Rust. But it seemed to want to guard me from things I wasn’t even trying to do, in anticipation that in some unrelated part of the codebase I’d suddenly abandon all of the values I’d been respecting and start interrogating into their bits. What if the “smaller Rust” is just “Rust with lexically scoped lifetimes unless you say otherwise”? I can imagine a borrow checker that defaults to the generally FP semantics of Rust’s syntax, rather than defaulting to the generally DSL-over-machine-code that’s typical of low level languages. I’m not sure what the language and implementation trade offs would be, but my instinct with Rust is that it would be simpler for most use cases, and not much more complex for the cases closest to C.
- kaba0 4y agoI don’t really see what you mean, perhaps you tried Rust many years ago? Because for stack-allocation there is zero lifetime annotations, it just works with as high-level code as you would get from a managed language. Rust’s complexity happens when you want to introduce non-tree-like memory patterns.
- rank0 4y agoAnyone looking for a pleasant systems language check out Nim. It’s the most fun I’ve had programming in a long time!
- himujjal 4y agoI like Nim. WASM is not natively supported by the core team, and the biggest of all. Cyclic Imports. Rest, its a great great language. If these two issues are resolved. I am all in for Nim
- sekao 4y agoNim supports wasm. You can target it with `--cpu:wasm32`. You most likely want to pair that with emscripten. Here's an example: https://github.com/paranim/parakeet https://github.com/paranim/parakeet
- the__alchemist 4y ago> a C developer might say. “But Rust is still a very large language, and only growing larger over time.” The C developer would be right. Nice article. As someone who uses Rust on embedded, this is a concern I share - especially regarding async/await, which this article mentions specifically. I see my (and for others of a like mind) path forward is using an explicitly-defined-at-project-level subset of Rust... In the vein of what we see in C++. This use of a dialect has drawbacks for newcomers to a code base, but I prefer it to the alternative of not mitigating the downsides of a large language. Perhaps this prediction won't turn out to be true, and the Rust language will slow its expansion.
- kaba0 4y agoOther than banning some universally agreed upon deprecated feature from a project, I don’t see these dialects worthy. The rules also often fail to change with times, often causing the detriment of the project if anything (thinking of some of these rules making C++ projects stuck pre-C++11)
- masklinn 4y agoIt also makes a lot more sense in C++ where each feature can interact with every other feature in non-obvious ways, meaning you need to know which features are used in order to know how the program behaves.
- v3ss0n 4y agoWhy all the new languages aren't white space based lang like Python ? Why they still need braces , braces are always hard to read the code and all modern code editor can work well with white-space based languages . A native , non inerpreted , high performance whitespaced lang with memory safety , as simple as python would rule .
- sebmellen 4y agoI find whitespaced code harder to read. Autoformatters like Prettier make brace-based langs really easy to read and parse.
- 5d8767c68926 4y agoNim?
- Schroedingersat 4y agoZig has a canonical format where braces match indentation. You could probably make some simple tooling such that you never saw braces.
- flohofwoe 4y agoThat's entirely subjective, there are a lot of people who have the same negative opinion about 'whitespace matters' languages. I switch between Python and 'curly brace languages' all the time, it's really not an issue after the first three hours or so (but I still miss sometimes that I can't throw in temporary debug code into python with different intendation so that it stands out). Same with semicolons btw, some people make a big fuzz about it, but in the real world it really doesn't matter, especially with how common real-time linters are nowadays (which helps me a lot because I sometimes accidentally end a line in python with a semicolon).
- ghoward 4y agoBecause code formatters cannot work on whitespace languages like Python. They can't adjust the indentation for you because they have to assume that the given indentation is intended. It also makes a lot of other automatic tools for dealing with source code harder to write, if not impossible.
- baby 4y agoReading this, I can totally see why I fell in love with Golang. The language is so dead simple that it's generally a pleasure to read any codebase, and very fast to learn/write. That being said I write mostly in Rust these days and really like it too. There's definitely a number of issues in Golang that I wish they would address (sum types for example)
- scoutt 4y agoI liked the article, and as an outsider to the discussion (I do embedded C for a living, and C++ as in "C with classes") I might add: What I've seen so far is that Rust tries to lure C developers with a sort-of "memory-management shaming". I've even been called "old curmudgeon" here in HN for not showing interest in Rust, and not using it right now. I think if hear one more time that "70% of bugs are memory bugs; here is the link" I'm going to explode. For which I answer: "My applications have a button and a display, what exploits are you talking about?". Spoiler alert: "memory-management shaming" doesn't stick with us. C is vast and has tens of millions of applications. Not every project is wired to Ethernet, BT or WiFi. If there is a bug, it might be hard to find, but that's it. Hey, there is people that even pays you to solve problems you've generated yourself. What's better than that? Luring C developers is about something else and Rust doesn't get it yet. At the same time Rust tries also to lure millions of high-level languages developers with features like async-await, super clever one-liners, package management, etc., which personally as a C developer, I don't really care. You can't make everybody happy. Make happy either C developers or JS developers. Pick one. Another note: even if the language it's been there for a while, I often see "hey! I've built a build-essentials replacement! It only works like the 20% of it". You go to see the repo and it's been idling for the last 6 months. So even the Rust's momentum is already starting to decay, there are really just a few of top "99% complete" reference projects. What does it means? Zig on the other side, shows itself as a more humble language, with simpler syntax. I don't really like the syntax but it's not a big deal. Zig has, or at least, it users don't show bigger expectations. It's taking a careful and slow road and I like that very much. That said I'm following both languages very closely. I only hope Zig doesn't become as bloated as C++ and Rust today, to be really in that sweet spot I'd like for embedded.
- childintime 4y agoI'm more or less in the same professional spot as you. Responding to your criticism of Rust all I can say is that you can only convince yourself, try it, if it isn't rhetoric you're interested in. Suddenly your code, when it runs, is bug free. It's a magic experience. More importantly it gives the ecosystem superpowers. That's an indirect response to your: > If there is a bug, it might be hard to find, but that's it. Which is damning. But typical of embedded. It's such a C shit hole. We debug most of our time, many of the tools are crappy. So you perhaps constrained yourself to 32-bit ARM devices, isolated yourself as much as you could, and created your own comfort zone? That's a symptom. There are many advantages to Rust, that run from things as basic as assuring code runs the same on a 8-bit MCU as it does on a 64-bit PC, to the trust one can have in the ecosystem, and the hive mind behind it. That said, though I see Rust becoming inevitable because of these reasons, I would very much like Zig to succeed, as it uniquely has the flexibility to construct the optimal solutions embedded craves for. Rust is more rigid in that sense, so Zig appeals more to the creative of minds.
- fdsafdsfdsa 4y agoWe get that Zig doesn't have memory safe features. How much of a big deal is that anyway - what proportion of issues are caused by memory safety errors? And as an aside - how many Rust codebases use unsafe code? (I don't know the answers to these - I'm a humble C programmer who intends to look at Zig soon).
- frankjr 4y ago> The Chromium project finds that around 70% of our serious security bugs are memory safety problems. https://www.chromium.org/Home/chromium-security/memory-safety/ https://www.chromium.org/Home/chromium-security/memory-safet...
- masklinn 4y agoSimilar observation at MS: https://msrc-blog.microsoft.com/2019/07/16/a-proactive-approach-to-more-secure-code/ https://msrc-blog.microsoft.com/2019/07/16/a-proactive-appro... > ~70% of the vulnerabilities Microsoft assigns a CVE each year continue to be memory safety issues That’s actually an understatement, on the graph with this caption 70% is closer to the low ebb / minimum in a given year (2012, 2018). During “bad” years (2009, 2014) it climbs as high as 80.
- AnIdiotOnTheNet 4y agoIt's worth noting when talking about how Zig isn't "memory safe" that comparisons to C aren't very illustrative because C almost goes out of its way to let the programmer shoot themselves in the foot. Zig was designed with a lot of features to prevent the kinds of errors C damn near encourages.
- Nomentatus 4y agoFor a couple of years, I’ve been thinking about whether there could be a simpler language that gets rendered into safe Rust the way Nim is translated into C and compiled as C. I’ve concluded that it is possible, but only if there’s a substantial chunk of AI sitting “in between” that analyses the code you’re writing in the simpler language and queries you, the coder, from time to time. I’m not sure how coherent I can be describing what I have in mind, but for example: let’s say you’re happily typing and the AI chimes in to say to the coder: “Hey, it looks to me like you may be assuming, in what you’ve written so far, that your process A will always end before your process B. Maybe - maybe - in the real world that’s so. But I can tell you that logically it ain’t so. What Rust code do you want me to write, here: 1) one that just goes from your process A to your process C whether or not your parallel process B has finished or not, or 2) Rust code where B always waits for A, A always waits for B, or both wait for each other, or 3) unsafe Rust code that leaves this possible race condition intact ‘cause your sure- you’re willing to bet the company - that not just this version of the program, but all possible future modifications of it, are never gonna encounter a real-world instance of B being delayed? And do you want any telemetry (asserts) (”fries”) with that?” Exactly how the AI can borrow check without you doing much if any borrow checking is maybe harder to describe; but might involve informing you that the code you’ve written so far allows simultaneous updating of a variable, and what would you like to do about that, if anything? Would you like some telemetry to let you know if such nonsense begins to happen during a run? (Fleshing this out may be non-trivial.) The AI would also bring stuff like badly formed loops allowing buffer overwrites to your attention, of course. I’m quite sure that creating such an AI programming helper/translator into Rust would be difficult. But I suspect it’s possible, and I want one. I’m retired, I won’t be building it. See verdagon's very interesting comment here (and the replies to that) for what's already being done at least a little along the same lines.
- Kukumber 4y agoMy problems with Zig: - the language ergonomics sucks - syntax to play with arrays sucks (zeroing for example) - lack of operator overloading for my math types - too many repetition and casting, the compiler is not smart at all - slow build speed Things i love: - build system, all in zig - comptime - module is basically a struct, so you can wrap your stuff in a file and import it as a struct, lovely - the language remains simple enough to pickup, learn and to build any kind of software, it is enabler Overall it's way too rigid, i miss the fluidity and liberty of C It'll find its niche, it'll serve as good build system, but it definitely not a good contender for a C alternative I couldn't care less about the illusion of memory safety with rust, chasing that language is a waste of time imo, and it's very dangerous, we'll see ton of new developers thinking their software is safe because the borrow checker didn't complain
- SubjectToChange 4y ago> ... and it's very dangerous, we'll see ton of new developers thinking their software is safe because the borrow checker didn't complain Would new developers be capable of writing secure C?
- Kukumber 4y agoNo, but that's not the point of C The problem is Rust advertise itself as a safe language due to the borrow checker, wich is a false and dangerous claim
- brundolf 4y agoThere are different definitions of safety If what you mean by "safe" is "memory-safe", then you're wrong. It is definitely truthful to say that Rust code is guaranteed memory safe (within safe code sections, barring compiler bugs, etc). If what you meant by "safe" is "bug-free", then it never claims to guarantee that your code is free of bugs- that would be nonsense (though it does a lot to help out!), and I don't think people would get that idea.