10 ms·
I'm curious too, I'm quite familiar with Rust and never written any Zig in my life so I went digging through the source and I find the syntax remarkably similar
by simias 7y ago
I'm curious too, I'm quite familiar with Rust and never written any Zig in my life so I went digging through the source and I find the syntax remarkably similar for the most part. The only thing that stood out is that apparently you can drop the braces for single-line `if` bodies like in C whereas Rust makes them always mandatory but I'm firmly on Rust's side on this one.
The part where Rust can get really messy is when you involve generic programing and traits, especially when lifetimes are involved, but I couldn't find any obviously generic code in this codebase.
EDIT: after digging a bit deeper into Zig it looks like it uses C++'s style duck typing for metaprograming instead of a trait-based approach like Rust? It definitely removes some overhead to writing generic code but I'm not sure about readability and maintainability... But I'm going to stop here because I'm about to go full fanboy for Rust.
- dnautics 7y agoIt uses "comptime" which, loosely, is a compile time version of the language. I would say it's a huge improvement over C in that quite frankly string interpolation for a preprocessor is terrible. I can't compare to rust since my brain can't parse rust syntax; too many bells and whistles.
- sobeston 7y agoTraits are a part of the stdlib (under std.meta) as opposed to the language. This is unlikely to change
- swiley 7y agoIf you want more syntax weirdness: tab characters are illegal.
- dnautics 7y agoIt's linted out iirc. Is it really strange to mandate that code look explicit and have no ambiguous whitespace?
- simias 7y agoI don't mind opinionated coding style (my Rust integration scripts all enforce that stock "rust clippy" doesn't return any error) and I do think that using tabs for indentation simply doesn't work in practice regardless of how great they are in theory because hardly anybody uses them correctly (including the vast majority of code editors by default). It might be strange to back it straight into the compiler but I don't mind it. That being said it does make it even weirder to allow braceless ifs IMO, but that's bikeshedding.
- clarry 7y ago> I do think that using tabs for indentation simply doesn't work in practice It has worked just fine for decades for projects with millions of lines of code. It might not work for babby's first patch and how do I configure an IDE??
- simias 7y agoDepends of your definition of "fine". The linux kernel works fine indenting with tabs but it mandates 8-space tabs and a lot of code ends up looking borderline unreadable if you use anything else, meaning that the only thing they get by using tabs is slightly smaller file sizes. Besides if you want to enforce the 80column limit you have to standardize a tab width anyway. It's simply not worth it IMO. The pros are simply too small and inconsequential to justify not using spaces on new projects. Although now that automatic code formaters are becoming ubiquitous it really doesn't matter what you use in your editor I suppose.
- chungus_khan 7y ago> including the vast majority of code editors by default Which ones exactly? Not really a problem I've encountered often except when someone tries to mix both spaces and tabs, and in general editors are built with the existence of this very common character in mind. People tend to hit the tab key to indent anyway, and one tab char meaning one level of indent is perfectly intuitive and allows users to individually configure how large they want their indents to be.
- 7y ago
- BubRoss 7y agoEven weirder than that, it intentionally fails on \r\n newline, so windows text files straight up don't work by default.
- loeg 7y agoGood! Even Windows Notepad, the extreme example of "not a code editor editor," supports \n newlines now: https://devblogs.microsoft.com/commandline/extended-eol-in-notepad/ https://devblogs.microsoft.com/commandline/extended-eol-in-n...
- BubRoss 7y agoWhy is it good to force all your users to figure out how to set their text editors to do something different and unnecessary just because you refuse to do what essentially every other non toy language has always been able to do? How is a decision like that not a giant red flag of user hostility and bad judgement?
- loeg 7y agoIs your metaphor about Windows since XP or about Zig?
- BubRoss 7y agoThis isn't a metaphor. Zig has gone to more effort to make itself break for windows users than it would have taken to make it work and not cause problems. Zig says it wants to replace C and it puts out builds that purposely break by default.
- wtetzner 7y agoI'm assuming it's only illegal as whitespace? In other words, they're allowed in string literals, right?
- rhodysurf 7y agoCorrect
- cgh 7y agoReally? I might have to take a look at this language then. Making tabs illegal whitespace might very well be a proxy for other good design decisions.
- Bekwnn 7y agoBecause the language is attempting to have a standardized formatter. I'm all for it, personally.
- loeg 7y agoHonestly, I like tabs for indent in C, but still support this language choice by Zig. It's hard to do tabs wrong if you don't allow tabs at all. Python3 went part of the way to disallowing tabs completely, but instead only disallowed mixed tabs and spaces. (And in Python, it's a bigger problem as the indentation level is significant to the syntax.)
- flukus 7y agoUgh, I was planning to learn zig at one point but things like this mean I'm going to pass. This doesn't even seem to be in the documentation that I could find. Throw in their plans to add a package manager and it's own make system and it seems to be getting further and further from the C replacement I was interested in.
- pron 7y agoI wouldn't call Zig's comptime "C++-style." Unlike Rust, there's very little in Zig that is borrowed from C++. Zig's error reporting and comptime makes it easy to write arbitrary compile-time checks, so Zig uses a single construct and keyword, comptime, to replace all special instances of partial evaluation: generics, concepts/traits, value templates, macros and constexprs. The main difference between Zig and Rust is a huge disparity in language complexity. Zig is a language that can be fully learned in a day or two. Rust has the same philosophy of "zero-cost abstraction" as C++, i.e. spending a lot of complexity budget to make a low-abstraction language appear as if it has high abstraction. Zig, like C, does not try to give the illusion of abstraction. There is also the difference in their approach to safety, but that's a complicated subject that ultimately boils down to an empirical question -- which approach is safer? -- which we don't have the requisite data to answer.
- gw 7y agocomptime does not replace macros though...there is no way to manipulate AST in zig, nor will there ever be, according to the author.
- pron 7y agoWell, it is intentionally weaker than macros (and I agree with Zig's designer that that's a very good thing, though it is a matter of taste), but it does replace many of the cases where in Rust you'd have to use macros (or the preprocessor in C/C++). So it replaces macros everywhere where it deems their usage reasonable.