by simias · 6y ago · 0 comments
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.
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.
We do have the requisite data to answer whether preventing use-after-free is better than not preventing it.
You can argue (not successfully, in my opinion) that it's not worth the loss in productivity to prevent UAF and other memory safety issues, but it's impossible to argue that not trying to prevent UAF is somehow safer.
I expect Zig will prevent use-after-free. It will be sound for safe code and unsound for unsafe code (by turning this on only in debug mode for testing).
> but it's impossible to argue that not trying to prevent UAF is somehow safer.
First, see above. Second, it is not only possible but even reasonable to argue that not trying to completely eliminate a certain error is very much safer. The reason is that soundness has a non-trivial cost which can very often be traded for an unsound reduction in a larger class of bugs. As an example, instead of soundly eliminating bugs of kind A, reducing bugs of kinds A, B and C -- for a similar cost -- may well be safer.
There has been little evidence to settle whether sound elimination of bugs results in more correctness than unsound reduction of bugs or vice-versa, and it's a subject of debate in software correctness research.
> As an example, instead of soundly eliminating bugs of kind A, reducing bugs of kinds A, B and C -- for a similar cost -- may well be safer.
Hasn't this essentially been what C++ has been trying for memory safety for decades, without success? The C++ approach has been "smart pointers are good enough, and they prevent several other problems too", and the experience of web browsers (among others) has pretty much definitively shown: no, they really aren't. For memory safety, I would not bet on this approach.
What kind of bugs do your have in mind that Zig would prevent and Rust doesn't ? I can think about several Rust does and Zig doesn't thanks to its afine type system, but I've yet to see a safety feature benefit in Zig.
It might be possible. Someone might prove that preventing UAF necessarily increases complexity somewhere else. Like one of those laws of thermodynamics, but for software.
For example, I wrote a PEG-like parser combinator library in Zig. Using it currently [looks like this](https://github.com/CurtisFenner/zsmol/blob/87de4c77dd8543011...). However, as a library, I ^could provide a function that looks like
pub const UnionDefinition = comb.fromLiteral(
\\ _: KeyUnion
\\ union_name: TypeIden
\\ generics: Generics?
\\ implements: Implements?
\\ _: PuncCurlyOpen
\\ fields: Field*
\\ members: FunctionDef*
\\ _: PuncCurlyClose
);
etc. But, I find reading the code as it is good enough for now that I didn't want to spend time implementing such a library.[^]: Being able to create brand new types at `comptime` isn't [yet implemented](https://github.com/ziglang/zig/issues/383), so this can't quite be done yet, though you could fake it with `get`/`set` methods instead of real fields
For many domains, being able to implement a domain-specific language with a set of expressive rules for the domain is an incredible productivity boost and also prevents many mistakes because you cannot represent them.
Not being able to manipulate the AST means that you are restricted on the embedded DSLs that you can provide. And embedded DSLs encompasses code generation for:
- state machines
- parsing grammars (PEGs for example)
- shader languages
- numerical computing (i.e. having more math like syntax to manipulate indices)
- deep learning (neural network DSL)
- HTML templates / generators
- monad composition
There is a reason most people are not building in Assembly anymore, there is a right-level of abstraction for every domain. A language that provides building block for domain expert to build up to the right level of abstraction is very valuable.
It's very easy to come up with examples of terrible abuse of these features that lead to bad code (like for instance if somebody was insane enough to overload the binary shift operator << to, I don't know, write to an output or something) but it also gives a lot of power to write concise and expressive code.
That being said it does make it even weirder to allow braceless ifs IMO, but that's bikeshedding.
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.
for example ('-' is tab, '.' is space):
--function_with_lots_of_arguments(arg1, arg2, arg3,
--................................arg4, arg5, arg6);
it can be done, but a lot of editors get it wrong and it requires paying attention to the whitespace.of course, another style would be to just indent the continuation twice, without aligning it. (i personally prefer to align continuations.)
>Not really a problem I've encountered often except when someone tries to mix both spaces and tabs
Mixing spaces and tab for indentation and alignment is how it should be done if you want things to remain aligned when you change the tab width.
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??
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.
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.