7 ms·
Looking at Zig Programming Language
- loeg 5y agoSeems to be getting hugged to death. https://web.archive.org/web/20220405142923/https://codecs.multimedia.cx/2022/02/looking-at-zig-programming-language/ https://web.archive.org/web/20220405142923/https://codecs.mu...
- skywal_l 5y agoTLDR: The author discuss its subjectivity towards the language. In short, they're not a fan of how zig handles errors, some loop constructs and the language design regarding keywords and built-in functions. They will not use the language even though it comes with interesting innovations. On the first two points, it's disappointing not to see a discussion on the goals of zig and why these choices were made [0]. Like any trade-off, there is a reason behind it. The third point, which occupies the biggest paragraph, is somewhat irrelevant to me. An interesting take but which remains at the surface. Might not be worth the 5 minutes it takes to read it. [0] https://ziglang.org/learn/overview/ https://ziglang.org/learn/overview/
- synergy20 5y agowhy is this downvoted? I like the short description. I spent a few hours two weeks ago to walk through ziglang's tutorial(to see how fit it is to replace c/c++ for system programming), my summary so far is: interesting stuff, but not good enough for me to switch yet. Will be on my watch list for next few years. Did similar thing with rust(ziglang to c vs rust to c++), decided to stay with modern c++ but will keep an eye on rust as well.
- karmakaze 5y agoThis was my first assessment for my own use as well. Seems very much a 'kitchen sink' language that has everything, but nothing in particular that draws me to it. If I had pick out one differentiator it would be that the weird case/camel-insensitivity makes integrating convenient.
- aserafini 5y agoWhat case/camel-insensitivity are you referring to?
- nrclark 5y agoAre you possibly thinking of Nim instead of Zig here?
- karmakaze 5y agoThank you, you are so right. I was thrown off by seeing a mix of snake_case and camelCase. I'll have to give Zig a read and make an actual assessment. Glad for the possibilities!
- karmakaze 5y agoI'm really digging what I'm reading so far. if (optional_foo) |foo| { doSomethingWithFoo(foo); } ..could be flow sensitive with optional_foo being non-? inside the block like some languages have done. Well the In-depth Overview ticks all my boxes giving Zig worth a deeper look: > Zig 0.5.0 introduced async functions. This feature has no dependency on a host operating system or even heap-allocated memory. That means async functions are available for the freestanding target. > Zig infers whether a function is async, and allows async/await on non-async functions, which means that Zig libraries are agnostic of blocking vs async I/O. Zig avoids function colors. > The Zig Standard Library implements an event loop that multiplexes async functions onto a thread pool for M:N concurrency. Multithreading safety and race detection are areas of active research.
- nrclark 5y agoIt's got really cool metaprogramming features too, if you like those. Kind of like C++ consteval/constexpr but on steroids. Language BDFL doesn't recommend using it in prod yet, but I've really enjoyed a couple of hobby projects.
- AnIdiotOnTheNet 5y ago
- roblabla 5y agoTiniest of nits to pick but: > and I wish rustc would have a stand-alone assembly files support Rustc now supports inline assembly and global assembly in Rust 1.59 (the very latest release). So this is now possible: /// Lib.rs file global_asm!(include_str!("asmfile.s"), options(raw)); Which will cause asmfile.s to be assembled and included in the rust library.
- JoshTriplett 5y agoAbsolutely, and any time you can use the built-in asm support, I'd recommend using it. But also, I'd love to see support for C and assembly files in rustc.
- wyldfire 5y agoNote that a really common use case for assembly files (.S files) is to expect that the file be transformed with the C preprocessor. It would be interesting to imagine a replacement with a rust equivalent but I can't help thinking that it's difficult or impossible to come up with an architecture / exec format independent replacement (other than m4). gas and cc1as support .macro directives - maybe that would suffice? But compatibility with existing assembly source files would suffer.
- anonymoushn 5y agoRegarding error handling verbosity, you don't actually have to write _ = file.write(data) catch |err| { silence_err_somehow(err); ... }. You can just write _ = file.write(data) catch { ... }. And the first example of using `catch` in the docs[0] does this. It may be useful to also use the non-error return value of `write` though, because partial writes will return an amount of written bytes less than the amount you asked to write. You need `writeAll` if you want to ignore this concern, and then you don't have to write `_ = ` because the non-error part of the error union is void (so you don't have to use it). Then you get file.writeAll(data) catch { ... } [0]: https://ziglang.org/documentation/master/#catch https://ziglang.org/documentation/master/#catch
- nonsequitur 5y agoHint: It's `catch {}` instead of `catch { ... }`.
- anonymoushn 5y agoThat's right if you want to do nothing on error. I think TFA uses `...` to represent the code they would actually want to run if an error happened.
- kristoff_it 5y agoWell, if those are the biggest problems the author has with Zig, then we're doing a fine job :^) WRT having error unions just be a userland tagged union type, here's a reason why we don't do that: https://ziglang.org/documentation/master/#Error-Return-Traces https://ziglang.org/documentation/master/#Error-Return-Trace...
- zozbot234 5y agoRust implements this at the library level via common crates like 'anyhow' and 'thiserror', on top of a tagged type and a standard "Error" trait. The latter was recently extended to allow for implementation of these features.
- lhorie 5y agoThe error thing to me hints more at how people use errors in the wild than anything specifically about Zig per se. The example that comes to mind is propagating parsing errors from a recursive descent parser. Typically one just wants to unwind the stack with some user friendly string error message indicating what exactly went wrong, but philosophically speaking, a parse error isn't really exceptional, it's an expected code path for invalid input. The way zig seems to think about errors is that early returns via `try` shouldn't be leveraged as a control flow mechanism to bail and bubble up values across the call stack. I think rather than saying why the error handling is done the way it is, it'd be more valuable if there were documentation on how one is supposed to go about coding to be able to use this bail-to-bubble-up-values pattern. Currently, the workaround is indeed somewhat verbose as it requires mimicking the `try` mechanism (and/or using some sort of global error store or whatever).
- anonymoushn 5y agoI didn't really understand. What is the problem and what is the workaround? Can you give an example?
- defen 5y agoThe problem (which is a deliberate design decision) is that Zig's error unions, which have special built-in support at the syntax level, are just tagged unions with an integer payload. In other words, when you return an error (either implicitly via `try` or directly with `return error.MyError`), that error payload is just an integer indicating which error was returned. So to use OP's parser example, there's not a built-in way to attach some sort of diagnostic info to the error itself, the way you would with, for example, python exceptions. Using the built-in syntax, you can't do the equivalent of `raise ParserException(token, line_number)` and then unpack that error later in your code to display a nice error message. The workaround would be to store that info in some sort of global struct (which has all the usual drawbacks of global variables); add a context/diagnostics parameter to your function, or manually manage your own error union in userspace, which means you lose the nice `try` syntax.
- verdagon 5y agoI made a tiny roguelike game in Zig a few months back and was really impressed. One thing I really appreciate about the language is the emphasis on keeping things simple. I heard someone put it really well yesterday: when you're programming in Zig, you spend your time tackling the original problem, not tackling the language. Also, the article's complaints may be valid, but most of them are very superficial, which speaks to good language design, in my opinion.
- anoncow419 5y agoDo you have a public repository for said game you published?
- sk1pper 5y agoHmm, I didn't find that quite to be the case. I wrote a toy GPU-accelerated (using OpenGL via C) terminal emulator in Zig. My take is that I'll probably wait for at least a 1.0 before I start hacking my side projects in it again. My main issue was around finding good documentation and (not-outdated) examples, which I guess just comes with using a bleeding-edge language. The biggest thing I find lacking is a "Learn Zig" book that takes you through the whole language. Sure, https://ziglearn.org/ https://ziglearn.org/ exists, but it needs about 10 more chapters. Zig types can get somewhat complex, especially when interfacing with C, for example - I remember some difficulty deciphering those. For many things I ended up just reading Zig's source code itself, especially when looking up standard library stuff. I mean I get it, who wants to write a book for a language whose features are unstable? On the plus side, I agree that I fought Zig a _lot_ less than when I first started learning Rust. And I still fight Rust if anything involving generics and/or nontrivial annotations is involved.
- exdsq 5y ago> you spend your time tackling the original problem, not tackling the language. Would you say it's as good or better than Lisp?
- Shadonototra 5y agoYou should take a look at D, if all you want is C/C++ like code, then you can focus on -betterC and use the libc or your own batteries (like i do) It is the perfect language, don't sleep on it!
- throw_m239339 5y agoD had its chance. It made the mistake of trying to be a paid language (like Rebol) and failed at it, because clearly unless you're target a niche but lucrative industry, languages with a paid compiler are mostly a failure in the era of GCC and co. It's too late to develop a significant community around D. Rust clearly got the community aspect right.
- Shadonototra 5y agoRust is a failure because it takes an eternity to build projects, and is now infected by NPM-hell dependency management Imagine you have to wait for hours for your tests to compile and then to run, and then your micro services to compile to deploy a security update for a major CVS Rust is poorly designed with poor syntax with a poor foundation that can't scale in today day and age
- the__alchemist 5y agoI've been doing a lot of translating C to Rust lately, and the Rust variants are much cleaner and more explicit. Especially regarding namespaces (ie no need to name_space_name_every_data_structure), explicitly declaring array types and references of instead of using pointers, and state management.
- Shadonototra 5y agoEverything 'RE'written in X, Y or Z will look better because the problem was already solved, you only have to rewrite the text
- 5y ago
- jmull 5y agoEh, this is an entirely superficial take. (Pattern match: not just like the things I currently already know.) I don’t think this has any value. (Not that there might not be valid criticisms of zig or insights into its value... But this article doesn’t contain them.)
- ajkjk 5y agoIt all seemed like valid criticism to me. It wasn't at all pattern-matching to things they know, it was evaluating and critiquing in each in term, and preferring one or the other for various reasons.
- GeorgeTirebiter 5y ago"No idea though where pointer dereferencing as foo.* = 42" I have no idea either, but it does remind me of BLISS; I remember there being BLISS-10 (for pdp-10) and BLISS-11 (for pdp-11), came out of CMU. (Apparently there were even more versions https://en.wikipedia.org/wiki/BLISS https://en.wikipedia.org/wiki/BLISS ) All variables were pointers; and so a common BLISS statement would be: .Z = .X + .Y which in 'C' would of course be z = x + y
- deleted 5y ago[deleted]
- nemo1618 5y agoI actually love the foo.* syntax. Think about it like this: if foo were a struct, then foo.x would give you the x field. But what if you want all the fields? Then you'd write foo.* -- a Unix glob!
- gsliepen 5y agoInterestingly, you can do foo.operator*() in C++: https://godbolt.org/z/f5Geqcvdj https://godbolt.org/z/f5Geqcvdj
- GeorgeTirebiter 5y agoThere is a small bug there, should be: Z = .X + .Y Since variables are names of addresses, you have to de-reference the X and Y addresses, add them, and, (crucially), the result gets stored: in the place pointed to by Z --- you don't care what .Z is anymore, as you are about to overwrite it. When you think of it that way, z = x + y seems 'wrong' because the meaning of a variable name is different on the LHS and the RHS of '='
- mc4ndr3 5y agoZig's ticketing moderation staff are humorless, gatekeeping stiffs. No thanks.
- xtian 5y agoSorry- what?? Is the implication that you made a joke issue and are upset about how it was handled?
- infamouscow 5y agoWhen tending a garden it's good to pluck weeds.
- Dork1234 5y agoGiven the current political landscape Zig really needs to change there logo.
- speed_spread 5y agoNyet, other guys change logo. Zig not yielding. Zig strong. If start moving over anytime some fucker claims ownership of some symbol, you're gonna have a bad time. Much easier in the long term to stick to your guns (eh) and make it clear that you 1 - were there first and 2 - have nothing to do with those little сука
- protomyth 5y agoGiven we have a tipi that has a symbol that looks like something some jack_ss German used, I agree. Being there first needs to be respected. Damned if you should let the evil doer win.
- Tozen 5y agoInteresting. Perhaps the writer might find Vlang (https://vlang.io/ https://vlang.io/, https://github.com/vlang/v https://github.com/vlang/v) to his liking. But many of these newer languages haven't hit 1.0 yet, so are in alpha and beta stages, so it's hard to say how the finished product will be embraced or how popular they might end up becoming. In many cases, these languages are alternatives to C++ (because of how messy it has become) and are positioned as a kind of "next step evolution" of C that also makes interop with it easier. Though much older, we could put D in that category as well. I don't think the audience of the newer languages is so much for hardcore C or C++ programmers. The newer languages would be more attractive to newer programmers, those coming from scripting languages that want to learn a compiled language, or older casual programmers actively looking for something different that still has C-like syntax. Otherwise, much older (and battle tested) Delphi/Object Pascal (which is about as powerful and as fast as C++) would be on more radars for switch-over, if not for the excessive C-like syntax bias.