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.
I’ve yet to meet one who actually didn’t end up with some safety issue in their code. Not all of their code mind you, but even a couple times can be significant.
Like you, I like memory safe languages because they let me think about my problem and trust the tooling to catch entire classes of mistakes for me
The claim I've heard (and I think also made) is a bit different from that. It's more like: "dealing with memory corruption issues isn't a significant portion of the debugging effort in [idiomatic] C++ code". i.e. the claim is not that you somehow won't run into them; rather, the claim is that they form a small fraction of the actual bugs you run into (either count-wise or time-wise), if you're writing C++ "properly". (If I had to make up a number, I'd say < 5%.)
This obviously isn't to say memory safety isn't an issue, or that security is a negligible property. Rather, it's to point out that most issues are of a different kind, and if you think that memory safety will decrease your workload significantly, that's unlikely to be the case if you're writing C++ with the correct idioms.
I think this response is usually in response to claims about how (say) a language like Rust results in N times faster development than C++. If that's truly the case, it's unlikely to be due to memory safety per se, but due to other factors.
I worked on a very large C++ codebase that compiled to webassembly, which runs sandboxed in people's browsers. It only talked to one (trusted) server.
Like any project, we had plenty of logic bugs every release. They would often result in odd artifacts on the screen and sometimes a crash.
Only 5% of our bugs were related to memory unsafety, and they also resulted in odd artifacts on the screen and sometimes a crash.
In other words, memory unsafety wasn't much of a concern in practice. It was as bad as any logic bug.
This was the main reason we stuck with C++, rather than spend SWE-years migrating to a more memory-safe language.
(I know this is ironic coming from me, who designs a memory-safe language full-time!)
From security risks to simple crashes, C++’s unsafe nature has caused a lot of issues in production in my experience. From renders and apps that crash to subtle security bugs.
Not having to focus on those when shipping software to customers means I can focus more on the actual logic and the associated bugs instead. I’m not getting back crash reports and CVEs that sucks up my time.
My users in turn aren’t getting affected by my crashes and security issues. Every person who reviews my code can also understand the language guarantees and stop passing my code through that lens and instead focus their mental energy on the other aspects of my code, like catching logic bugs.
The effects of a memory safe language extend very far
Here's one of the efforts in this direction (for anyone who hasn't seen it): https://www.chromium.org/Home/chromium-security/memory-safet...
Zig is vastly safer than Rust unsafe. That's worth noting.
So while Rust's memory safety features is something I love, I absolutely hate the Rust community pushing mem safety as a panacea.
In my line of work, as an embedded programmer, Zig is vastly more appropriate.
Rust and Zig can co-exist just fine.
> I'm very skeptical that the lack of memory safety is not going to be an issue "in practice".
For what it's worth, I agree with you. And I'm a C programmer that managed to put out a non-trivial piece of C code ([1]) that has had no memory safety issues triggered in the wild after 1.0 (which was released on 26 Oct 2018, so almost 4 years, yay!) that I know of.
The truth is that I am not able to write such code; I just fuzz the ever-living daylights out of my code with lots of asserts to test all of the assumptions that my code makes, including preconditions, postconditions, everything. I also use static analysis, -Weverything on Clang (without -Wpadded; that's a stupid warning), and a massive test suite with near 100% coverage. In my new projects, I'm even using fuzzing to generate almost all of my test cases so that my path coverage is as good as possible.
That's a lot of work, and it's not fun.
But what would make it even more effective would be to effectively automatically add asserts for me to test the assumption that an index is in range whenever it is used! And that's why memory-safe languages are the only way to go: not only do they save people from bad code, they help programmers turn bad code into good code with the tools at their disposal.
I think the Zig people know this, which is why they have ReleaseSafe for testing and for those cases where you have to be sure. But I also don't see them test their code enough for me to trust that it's not needed. And I doubt that Zig users test their code enough for it too.
All of this is to say, I completely agree with you despite (so far!) being the exception to the rule you articulated, and I dread the day when my reign of "being the exception" comes to an end. I really want to stay being the exception.
ReleaseSafe is supposed to be the 'normal' way to compile (the same role O2 takes now albeit with a performance penalty compared to O2), with releasefast or releasesmall for those times performance is unacceptable and where you've satisfied yourself with the level of testing you've done.
The philosophy of the project is to build some of those static analyses and runtime checks that help fuzzing into the compiler and stdlib. And build a culture and toolchain where it's easy to start using the rest as part of the status quo (rather than them being the exotic thing used by weird people). Finally they get rid of some of the most egregious footguns ( ostly relevant to beginners), passing slices is much more ergonomic and less error prone for a beginner, the error handling is best in class, and there are optional types and less use of void pointers.
Of course doing the actual hard work is always going to be hard work. But you can turn it from hard and gruelling work where the majority of people have no idea of the things they should even be doing and where you uave to keep a laundry list of gotchyas in your head into hard but satisfying work that feels fathomable.
It's a different philosophy from rust, but one that is no less valid.
Footguns are never just relevant to beginners. I say this as something close to an expert C programmer. I hit footguns all of the time. I just don't release them because I always assume I hit them and hunt them down.
Making things easier to fuzz is table stakes; even C can do that now. I set up fuzzing for my code with a simple build option. It's not hard. It's also effective at finding test cases for you, if you use it right.
Does Zig do Design by Contract? That would make fuzzing easier. Does it do Property-Based Testing? That's a more focused version of fuzzing. In fact, it's the same to unit testing as fuzzing is to integration testing. Until Zig has Property-Based Testing, I don't believe it's making testing easier.
For that matter, as far as I know, Zig only makes unit tests easy. Unit tests are okay, but not really effective. The test suite for my code is only integration tests, which makes things easier to refactor. And yet it is effective at finding bugs.
tl;dr: What Zig has is table stakes in 2022. It is nothing special for helping people write correct code.
Adding gotos is not an upgrade to readability and debugability, and comefrom was supposed to be a parody.
The C equivalent would look like this:
do
{
err = some_operation_that_can_return_errs();
}
while (err != NULL && handleError(err) == ERROR_HANDLED);
if (err != NULL)
{
panic("Could not handle error: %s\n", err_str(err));
}
That is not goto or comefrom. Far from it. And of course, the panic at the end is not necessary; you could then return the error, or throw an exception, etc.Every explanation of conditions and restarts I can find explicitly mentions searching the call stack for a handler and jumping into it, then reserving the capability to return to the function the error originated in.
This is explicitly non-local and hidden control flow (ie. the much maligned goto of exceptions).
The only advantage I can see asserted of having handlers (exceptions or conditions) that way is that you don't have to look for the error and pass it up manually but ! does that anyway so it doesn't seem worth throwing out your ability to reason about invariants in the context of a language with mutability and such.
Then there's returning to the stack frame you started in (the part I somewhat unfairly called comefrom). You need to drag around all the machinery required for continuations in order to do this. If the goal is to have available some of the context of an outer stack frame, you could pass the relevant references down and just handle it where you wished to resume.
Further thoughts: In some ways errdefer is the polar opposote of this concept. It allows the function to clean up and preserve invariants whilst passing control up the call chain.
> This is explicitly non-local and hidden control flow (ie. the much maligned goto of exceptions).
No, it's a function call. That's how you jump to code while reserving the capability to return.
I mean, you explicitly "raise" an error, right? What's wrong with code like:
raise AllocationFailure;
desugaring to a function call? I thought function calls were local and non-hidden control flow, so why do you think otherwise?> The only advantage I can see asserted of having handlers (exceptions or conditions) that way is that you don't have to look for the error and pass it up manually but ! does that anyway so it doesn't seem worth throwing out your ability to reason about invariants in the context of a language with mutability and such.
One example is that perhaps you do want to ignore certain errors, but let others through.
I have code that returns an error on EOF. Usually. In reality, it calls an error handler and lets the handler decide.
When I open and read files to the end, I'll encounter EOF, but I want to. So I'll push a handler onto the stack of error handlers that ignores EOF but handles all other errors. If I did that with exceptions, the call stack would be destroyed. Not good.
Another example is memory allocation failure. Maybe one part of your program caches allocations and needs to be told to give them up if allocation failed. So in the handler for allocation failure, it calls a function to release those cached allocations, and when the handler returns, the allocator can try again. Hence, the restart.
> You need to drag around all the machinery required for continuations in order to do this. If the goal is to have available some of the context of an outer stack frame, you could pass the relevant references down and just handle it where you wished to resume.
No, you don't. If you did, I wouldn't have been able to implement it in C, but I did. [1]
> Further thoughts: In some ways errdefer is the polar opposote of this concept. It allows the function to clean up and preserve invariants whilst passing control up the call chain.
Passing control up the call chain on error should be a last resort because you're destroying context, but I agree, it should still exist.
The cool thing is that you can do this in C too, even with cleaning up and preserving invariants. See [2], [3], and [4].
[1]: https://git.yzena.com/Yzena/Yc/src/branch/master/src/concurr...
[2]: https://git.yzena.com/Yzena/Yc/src/branch/master/src/alloc/s...
[3]: https://git.yzena.com/Yzena/Yc/src/branch/master/src/alloc/s...
[4]: https://git.yzena.com/Yzena/Yc/src/branch/master/src/alloc/s...
This is exception semantics which is hidden control flow present in every language with exceptions. Even in checked exceptions it is often obfuscated (ie. a function call that can raise is not distinguishable from one that can't wkthout manually looking at the types, and if they are distinct, then you just have something equivalent to error types). The point is that, upon calling a function that might transitively call a function that raises, you have no idea if the next line will execute or whether you will wind up in some other piece of code altogether.
It's not equivalent to calling a function because it destroys the stack context and violates the invariants and ability to reason about control flow for every function above it in the call stack.
I am starting to grok algebraic effects, and how they can make exceptions-but-not-objectively-incorrect by preserving context during the interrupted control flow and returning everything how it was left, but they come at a price.
You could guarantee that the stack will be reconstructed and execution will continue where it left off. But now 'raise' implicitly does a bunch of stuff including allocating memory on the heap and copying every stack frame between your error and your handler onto it. How does this function if your error is that you are unable to allocate?
Even then, you don't know where control will end up without hunting for the registration of the handler, which may not even be near the call site.
Sure you can build a stack on the heap in userland in C and dump everything there so as to reconstruct it (I skimmed your code and it seems like some implementation of coroutines or similar, I'm notnsure if this is part of the error machinery). There's nothing in zig that prevents you from doing this, or even using that exact library.
> The cool thing is that you can do this in C too, even with cleaning up and preserving invariants. See [2], [3], and [4].
I either can't follow your code with the amount of effort I'm willing to expend (likely) or there isn't anything resembling the descriptions I can find for conditions and restarts there.
Are you saying that the following can happen?
I can have some function foo that is regular C code with the exception of registering that it handles some condition.
Then foo can call, bar, which calls baz which calls qux, all of which are regular C code.
Then qux can call something which raises a condition of the kind foo registered.
Then control flow passes to foo which handles the error using the context in its current stack frame.
Then control passes back into qux with information provided by foo's handling code.
Then qux returns, followed by baz and bar returning without having to have any knowledge of or participation in your system.
And that all of this happens without constructing a closure over foo's context and sneaking it into qux via a global.
This is not exception semantics. A language can make the keyword "raise" do whatever it wants.
Should I have used some other keyword? Fine:
handle AllocationFailure;
In fact, let me expand it a bit: restart
{
err = allocate(size);
if (err != NULL) handle AllocationFailure;
}
This code calls the AllocationFailure error handler when allocation fails, and has the option of "restarting" (i.e., going back to the restart keyword and trying again). There is no hidden control flow, unless you think `errdefer` is hidden control flow.> It's not equivalent to calling a function because it destroys the stack context and violates the invariants and ability to reason about control flow for every function above it in the call stack.
No, it doesn't. Again, I implemented this in C. I don't use longjmp(). It does not destroy stack context. You seem to have no idea what you are talking about.
> You could guarantee that the stack will be reconstructed and execution will continue where it left off. But now 'raise' implicitly does a bunch of stuff including allocating memory on the heap and copying every stack frame between your error and your handler onto it. How does this function if your error is that you are unable to allocate?
Yes, by doing a function call. It's a function call. Really.
> Even then, you don't know where control will end up without hunting for the registration of the handler, which may not even be near the call site.
The registration of the handler does not need to use the stack. In my code, it uses a separate heap-allocated stack, which is in the thread-local data.
Did you not read the actual C code I sent you? It's all in there.
> Sure you can build a stack on the heap in userland in C and dump everything there so as to reconstruct it (I skimmed your code and it seems like some implementation of coroutines or similar, I'm notnsure if this is part of the error machinery). There's nothing in zig that prevents you from doing this, or even using that exact library.
It's not coroutines. It's simple function calls. It's using function pointers, yes, but it's just function calls.
> I either can't follow your code with the amount of effort I'm willing to expend (likely) or there isn't anything resembling the descriptions I can find for conditions and restarts there.
Because your mental model of conditions and restarts is completely wrong.
> Are you saying that the following can happen?
No, that is not what happens. This is what happens:
You have some function foo that is regular C code with the exception that it registers ipsom as a handler for some condition until foo returns.
So ipsom is a registered handler until foo is done executing.
Then foo can call bar, which calls baz, which calls qux, all of which are regular C code.
Then qux can call something which raises a condition of the kind foo registered.
Then control flow passes to ipsom, which gets a new stack frame, and which handles the error using the context in its new stack frame.
Then control returns back into qux with information provided by foo's handling code.
Then qux returns, followed by baz and bar returning without having to have any knowledge of or participation in your system.
And that all of this happens without constructing a closure over foo's context and sneaking it into qux via a global because foo's context is never used as the error handler!
Again, it's just function calls and a possible jump to the beginning of a marked block.
To show this, I have fully implemented restarts now ([1]), which I didn't do because I didn't need them yet.
Now, if you had a hypothetical foo that registers ipsom as an error handler, you would do this in foo:
y_REGISTER_HANDLER(ipsom);
// The rest of the code.
y_UNREGISTER_HANDLER;
I need to hook error handler registration into my RAII system so that you only have to register it (and it will automatically be unregistered on function exit), but you get the idea.Now, in the function that actually raises the condition:
y_RESTART(restart)
{
err = function_that_returns_err();
if (err != y_ERROR_SUCCESS) y_HANDLE(err, status, restart);
}
Of course, it's C, so the macros are a little ugly and requiring passing in variable names, but it works.In this code, if `err` is not a success value, then ipsom is called, which will either not handle the error, handle the error but not allow a restart, or handle the error and allow a restart. If the last one is what happens, the code will jump back up to the beginning of the y_RESTART block and start over.
Edit: I realized my commit forgot to add y_strucon_handleErrorWithRestart(). It was added in [2].
[1]: https://git.yzena.com/Yzena/Yc/commit/95312b059132dc3e8614b4...
[2]: https://git.yzena.com/Yzena/Yc/commit/61c4a3ef735446a9478c58...
You've made error values. This is just one trivial thing you can do with error values without adding keywords. As you said, it's just a function call.
This is the error system that languages like zig and rust have.
Ie. the best paradigm for error handling you can get without all thenoverheadnof a pure FP language.
Except Zig has a bunch of erganomic enhancements to make them far more pleasant to deal with and reduce the mental overhead and scope for mistakes. You can't get the value out of a failable function without acknowledging and passing up or handling the error, and you don't need the verbiage of if err != nil adding noise everywhere. And it comes with batteries included so you're not going to use code that uses something stupid like errno instead without noticing. And you get errdefer to mitigate one of the biggest downsides to saying 'I can't handle this here, passing an error up'.
They can't carry arbitrary data, because where would you put it in a language that has an error system capable of treating allocation failure as a handleable error? There's nothing stopping you from putting some state or context elsewhere, but you have to make those decisions.
Similarly you can have the error get handled in a parent scope without unainding as I demonstrated if that's really really important, but it shouldn't be the default.
Well, yeah, with some "ergonomics." Like walking a stack of error handlers. Across threads. Go back and read my code; that's what it's doing: a stack walk across threads, followed by a jump backwards depending on certain conditions.
Does Zig do that? Not builtin, for sure.
Could you implement that in Zig? Sure, but then you're outside its "best-in-class" error system.
> This is the error system that languages like zig and rust have.
So you're admitting that Zig does not have any better error handling than C? Because C has return values.
At least Rust lets you use Option values and Result values.
> Ie. the best paradigm for error handling you can get without all thenoverheadnof a pure FP language.
Sigh...I just implemented something from a functional programming language in C. Overhead is minimal.
> Except Zig has a bunch of erganomic enhancements to make them far more pleasant to deal with and reduce the mental overhead and scope for mistakes. You can't get the value out of a failable function without acknowledging and passing up or handling the error, and you don't need the verbiage of if err != nil adding noise everywhere.
That's just manual exceptions with the compiler complaining if you get it wrong. I mean, sure, it's great that the compiler will check your work, but Clang and GCC can give me warnings about unused return values, so...that's not much better than C.
And they are still exceptions. You're still destroying stack frames until some function higher in the stack handles the error. When it does, all of that context is gone.
Using conditions and restarts means that you can make the decision to tear down the context after* handling the error. Exceptions are much less worse in that case. I've actually found them to be useful in certain cases, but only if the error is handled first.
Also, you can add those ergonomics in C. Like so:
#define try(var, expr) do { typeof(var) ret_ = (expr); if (is_err(ret_)) return ret_; var = ret_; } while (0)
> And it comes with batteries included so you're not going to use code that uses something stupid like errno instead without noticing. And you get errdefer to mitigate one of the biggest downsides to saying 'I can't handle this here, passing an error up'.I will say that errdefer is a good idea. I did implement basically that.
But no, Zig's error system doesn't come with batteries included because it can't carry arbitrary data. And it doesn't do a walk of a stack of registered handlers. And it doesn't do restarts. And it doesn't have Option or Result, nor interfaces so you could implement the same thing for other types.
Whatever you might think of conditions and restarts, if you seriously think that Zig's error system is best-in-class when Rust's does the same thing and more, while also allowing user code and types to use the same infrastructure, I just don't know what to tell you.
And I'm no fan of Rust. But it's better than Zig.
> They can't carry arbitrary data, because where would you put it in a language that has an error system capable of treating allocation failure as a handleable error?
Preallocate on thread startup. This is not hard.
And on allocation error, every function that allocates is controlled by the language (through the standard library), so that's not a problem in practice. And even if something tries to wrap an allocation failure in more data, and allocation fails again, just panic(). Really. You can even document that behavior in the language.
> There's nothing stopping you from putting some state or context elsewhere, but you have to make those decisions.
Yes, but that's an admission that Zig's error system is not ergonomic. Yet you claim that it is best-in-class simply because it's slightly more ergonomic than C's?
Sigh...
It has both of those. The included error types are exactly result with added erganomics (passing up one frame is the easiest thing and is erganomically the same as checked exceptions)
Maybe learn what something is before arguing about it?
If the inner function yields either a value (or none) or an error, the outer scope handles the error amd returns control to the async function (optionally with state gleaned from the handler) you have all the pieces.
I believe yield lacks any kind of syntactic sugar (planned), but is possibly in current async functions.
Viewing it through that lens, I can see it as a viable alternative to eschewing exceptions altogether.
Here's my best attempt at demonstrating conditions and restarts with zig-native tools.
Edit: fixed my dumb. https://zig.godbolt.org/z/creoT47Mj
Note that it'd get substantially more erganomic if yield were finished with all of the usual niceties of the monad-ish error keywords.
Also edit: Upon further reflection to be actually correct conditions/restarts, you'd have to unwind starting from inside the inner function if the handler gave up. This is doable by passing some state back down (handled/unhandled) from the outer scope, and then just returning an error from the bottom scope to begin either unwinding or let a non-restartable error handling mechanism do its thing.
Additionally I don't think this is compatible with also using async for its intended purpose without comptime shenanigans.
In your example, I don't see any, which is great. But async can have hidden control flow. (I wonder why Andrew Kelley added it when he is so against even hidden function calls.)
On top of that, I don't think async is a good paradigm, but that's neither here or there to the conversation.
Ie. Zig's error system which is not based on algebraic effects is best in class.
Async does carry that same cost, but it comes with capabilities you can't get otherwise and is optional. Any objections to it are a demonstration of my point, not a disagreement with it.
Exceptions are better than async because they do not "reify" stack frames, an incredibly terrible idea. I mean, it works, but it's not a good model, mentally or otherwise.
Then you put your value behind the error in the type...like rust or zig where you cannot ignore them.
You keep describing the upsides of Zig's error system as reasons why something else is better.
On my experience, only small teams with top devs do happen to care for doing the security work you describe, as tends to be proven by surveys regarding the use of unit testing or static analysis tooling,
https://www.jetbrains.com/lp/devecosystem-2021/c/
https://www.jetbrains.com/lp/devecosystem-2021/cpp/
So yeah, pity that memory-safe languages have to fight back to regain what was already possible before UNIX took over the computing world and brought C for the ride.
This is why I would like that Zig could offer more than Modula-2 already did in 1978, in regards to memory corruption.
I think the lack of bounds checks where they are not a problem is a massive design flaw nowadays. Zig should have made bounds checks universal and spent effort on compiler passes to elide them.
Allowing people to remove bounds checks is not smart (though Rust's unsafe is a good compromise), especially in such a coarse way as a build option.
In fact, a supposedly memory-safe language, Julia, has memory bugs because it allows users to turn bounds checks off. [1] This should never have happened.
Here's a car analogy - old manual vehicles had unsynchronised transmissions where the clutch had to be depressed twice per gear change. Newer manual vehicles use steering wheel mounted paddle shifters and the clutch only needs to be depressed when stopping or starting.
You're trying to push your weird semi-automatic transmission - that a lot of people find unusable as a daily driver - by ranting about how awful double clutching is.
There are systematic reasons why I'm skeptical Zig's approach will lead to better results than what we see in modern C++. The worst memory safety issues from a security point of view tend to be use-after-free caused by complex, untested code patterns, where one programmer wrote, or implied, "free", without realizing that somewhere else in the millions-of-lines codebase some other part of the code, written by someone else, still had a reference to the object. Unit tests don't catch these problems because they're caused by strange patterns that the programmer didn't anticipate. That is the kind of vulnerability that is difficult to solve without either a compiler or a runtime system (GC) to help you, and I see nothing in Zig that solves it. You can, and people do, make raw pointers to other objects in Zig.
It's not trying to be for large, widely attacked projects. It's for things where the complexity overhead (whether resources or mental overhead) of Rust is too much, so you'd have to use C.
It's not meant to make memory errors impossible, it's meant to make them much easier to avoid or find and remove than it is in C.
Noone is suggesting anyone make a web browser in Zig, but it's likely useful for drivers or similar where you'd otherwise be using a lot of unsafe code, so rust's guarantees are at best a partial help, and at worst a false sense of security.
There is arguably no place for Zig to go, along the "better C" route, that hasn't been covered by other languages. And languages like D and Object Pascal, are way up on the charts, so it will be a hard climb to reach them in popularity. Even the much older Nim language (which has been called a C alternative), still hasn't cracked the top 50, among language rankings. Debatably, a language's "claim to fame" (to escape niche status), can't just be about being a more complex C replacement. There has to be other things about it, that it can firmly stand on, for greater mass appeal.
Object pascal appears to be pascal with classes.
Both of them have exceptions and operator overloading.
Zig is smaller than object pascal or D, and closer to the metal than D, it's arguably simpler than C as it's more regular, has no hidden control flow and has no separate preprocessor.
The unique things it's attempting to do are manual memory management with handrails (not dissimilar to pascal, but maybe with a few more handrails like testing allocator), error handling with better erganomics than go, rust, or unchecked exceptions (but slightly less power), and comptime which afaik is unique and is where it provides much of the power to do things like generics, many kinds of composition, or even implement a borrow checker and lifetimes as user code (with terrible erganomics and poor performance, but it's illustrative of its power).
The thing it stands on for mass appeal is trivial interop with C and the zig compiler is a functional, statically linked C toolchain out of the box and is easier for a beginner to do things like cross compile on than gcc or clang msvcc or similar. Additionally it has the power and simplicity of C with fewer footguns (ie. it attempts to cover some of pascal's strengths, but on the balance I like pascal more as a teaching language). It also shares some of nim's goals like fast compilation and hot code reloading.
The thing I'm personally interested in is the type system and semantics of comptime seem to have all the ingredients required for formal verification with no additions to the language, translations to a prover, or annotations (but it would require extending the compiler's abilities considerably). This is unique if true (but I will lack the skills to find out if it is true for a while yet, and the people closer to it are busy with other things).
Whether it will succeed, or the comptime concept is fatally flawed in some way is as yet unknown, but it does fill out a currently empty niche as a simpler C, it is not trying to be yet another more complex C.
I think its main competitor if it is successful will be go. Go is slightly higher level, arguably marginally simpler and considerably less powerful out of the box (at least last time I looked before the latest generics stuff). The last two are arguably in its favour for being a better C.
Zig is arguably in a different category and appears targeted to a more limited or lower space, in terms of general programming, which brings it more in the orbit of Rust. It's made clear that Zig's features and size will be purposely kept to the minimum, though its syntax is significantly complex. Which may hamper it, as it appears such complexity does to Rust, in terms of being picked up for general usage.
It's hard to say how attempting to be a "better Rust" or attempts at taking on C, will work out. But, it does seem clear it's not going for more general and wide usage that languages such as Object Pascal, D, or Go cover.
By the way, Object Pascal can be written where it comes quite close to classical Pascal, as classes are extensions of the language and more optional than required (see also advanced Records and/or Objects). Object Pascal is used in embedded systems, along with various dialects of Pascal, specific for the purpose. There are also various variants of Go, aimed directly at embedded development (TinyGo). So variants of Go and Pascal can more directly compete with Zig, and they are already well established (like Rust is). Now how this will all turn out, in say 5 years from now, anybody's guess. But, it will be an uphill climb for Zig, as for any new language trying to displace established ones.
Vlang documentation (https://github.com/vlang/v/blob/master/doc/docs.md)
Odin documentation (https://odin-lang.org/docs/overview/).
Golang by example (https://golangbyexample.com/)
I don't understand the basis on which you're saying this. From where I'm standing only odin looks simpler, the other two just make different tradeoffs -- namely trading reduced metaprogramming facilities (and the danger of complexity that entails on user code) for more language features.
> It's hard to say how attempting to be a "better Rust" or attempts at taking on C, will work out. But, it does seem clear it's not going for more general and wide usage that languages such as Object Pascal, D, or Go cover.
D and Go are closer to rust than zig is, and object pascal is also just another OO language. Of those, only Go is even attempting to be a better C, but requiring a runtime prevents it from being a true competitor.
Why bring this up?
There are plenty of tools in the language and ecosystem for dealing with memory errors and reducing their frequency, but you're clearly not interested in a rational discussion.
This makes something like the use of sanitizers required for large projects in Zig; It's a race against time until your project gets big enough that someone mistakenly frees something that gets used 200 lines later.
If a Zig user wants to restrict usage of pointers in certain ways let them make their own data structures, but unless Zig is willing to alter its semantics including a ‘standard’ smart pointer is putting icing on a cardboard box.
/edited to fix a typo/
I love Rust, but I think Zig solves 70% of the memory safety problem at the benefit of a much simpler language.
Ironically, the person I think you're referring to is actually me, and that's a strong mischaracterization of what happened. Nobody was ever "threatening to fork the language". Besides, that's irrelevant to this discussion, because none of the complexity of Rust compromises memory safety.
> I love Rust, but I think Zig solves 70% of the memory safety problem at the benefit of a much simpler language.
I don't believe the 70% number without evidence. Again, the problem that we really need to solve is complex UAF caused by unexpected interactions between components of large software systems, and I see nothing in Zig to solve that.
On a more serious note: IME most memory corruption issues in C and C++ are 'side effects' of the sloppiness of those languages (like not catching overflow, missing range checks, problems arising from implicit type conversions, sometimes just not compiling with warnings enabled, etc...).
Zig enforces much more correctness (to a point where it is sometimes annoying to a C coder, but one quickly gets used to it), and it even does this when building C/C++ code (just by using much more 'picky' default compilation options than C/C++ compilers).
I've been ranting about it many times. Most recently: https://news.ycombinator.com/item?id=32145520
There is an upstream issue about it where we should probably get our voices heard, it's encouraging to see many people in that thread that are opposed to that change, but it doesn't seem the core devs are open to considering any alternative, rather they just hope we can learn to live with it.
https://github.com/ziglang/zig/issues/335
This is by far my biggest annoyance with Go, more so than writing if err != nil.
I feel I write code in a different way than other people. Some are very slow, thoughtful and methodical in writing code, while I tend to experiment, throwing stuff at the wall and iterating quickly, and only after going back to polish and refactor. Stopping every iteration during my experimentation phase to lint my code because the compiler is pedantic and stubborn just slows me down immensely and turns me off a language.
EDIT: went and shared my criticism upstream, in a constructive and reasoned manner, I hope. My quixotic fight against unused variables being errors carries on.
Public discussion of it is much more effective.
Best case scenario, they take notice at the wider world meme'ing Zig for such irritating quirks and realise how many potential users they're losing. An OK case scenario is someone maintains a fork. Worst case scenario is they don't change, no one bothers with a fork, and very few people bother with Zig and general.
Ball is in their court. I would hate for them to cut their nose to spite their face, but I suspect they will unfortunately.
https://github.com/ziglang/zig/issues/358#issuecomment-49100...
for (var i: usize = a; i < b; i += 1) { foo(); }
for (var i: usize = a; i < b; i += 1) { bar(); }
Is worse than: var i: usize = a;
while (i < b) : (i += 1) { foo(); }
while (i < b) : (i += 1) { bar(); }
Which is what happens without the for-loop scoping of the variable. (ie when I cut and pasted, I introduced a logic bug where I forgot to initialize i for the second loop).https://github.com/ziglang/zig/issues/7257#issuecomment-1030...
Would you be willing to expand this a bit? This would save you from giving a dummy name in a function?
_ = myvar; // TODO remove
easily for all function arguments when prototyping.
I agree it can be cumbersome, but I am not sure it is a bad thing.
Should have been a warning. Baked in from day-one, easily makes that a CI check without hampering development.
When these languages tout fast iteration cycles, yet for a single-line code you have to do a recursive, n-step compilation of everything that might also get unused is literally insane and indefensible position.
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.