3 ms·
Is a C replacement (which is not also a C++ replacement) really what anybody wants? Like with no generics, no dedicated error handling, and no automatic cleanup
by oconnor663 5y ago
Is a C replacement (which is not also a C++ replacement) really what anybody wants? Like with no generics, no dedicated error handling, and no automatic cleanup? I get that everyone enjoys a simple language, but these features feel like table stakes now.
- sirwhinesalot 5y agoZig has all of those things, if you consider defer to be a form of automatic cleanup.
- afdbcreid 5y agoWhich it is not. It is harder to forget it, but it's possible.
- oconnor663 5y agoFor what it's worth, I think it makes sense to include defer in the "automatic cleanup" category. If we don't, then I think we end up suggesting that the only popular languages that support automatic cleanup for anything other than memory are C++ and Rust. That does have some truth to it, but if the goal is to describe "languages that are viable today", it seems pretty clear that e.g. Python and Go are viable.
- sirwhinesalot 5y agoTrue but most languages need something like it to manage non-memory resources. Stuff like Python's with block or C#'s using block. So Zig is on par with those for non-memory stuff and much much better than C. Having everything accept an allocator also makes managing memory a lot simpler since you can use a simple arena allocator for temporary heap allocations.
- Bayart 5y agoException handling and garbage collection are two features that feel superfluous, if not outright noxious, to a number of programmers; in particular those doing systems, embedded, real time programming. That is the crowd that still uses C. There's a cost associated with the execution environment taking control over from the program itself, and by proxy the programmer. It's not something you want when you design your code with the assumption that is it a more or less accurate representation of its execution flow. Long story short : those who want that sort of programming language know why.
- oconnor663 5y agoAgreed about GC and exceptions, but I wanted to focus on other approaches. Rust and Go are good examples of doing error handling through regular return values. Similarly Rust and C++ do cleanup with destructors, and Go does at least some of it with deferred function calls. There are ways to do these things that are suitable for low-level programming. But doing nothing no longer seems viable to me. To the extent that Zig's features in these areas (and also generics) make it "not a real C replacement", that's when I question whether a real C replacement is actually what anybody wants.
- loeg 5y ago> Go does at least some of it with deferred function calls. For what it's worth, Zig has a very similar deferred function call mechanism for deferred cleanup, and something like Rust's Result type. See https://ziglang.org/documentation/master/#defer https://ziglang.org/documentation/master/#defer and https://ziglang.org/documentation/master/#Error-Union-Type https://ziglang.org/documentation/master/#Error-Union-Type .