3 ms·
I think lack of objects/destructors is also a bit of a minus. Manual memory management is hard to get right, and if you're not going to go the Rust route, I thi
by wcrichton 8y ago
I think lack of objects/destructors is also a bit of a minus. Manual memory management is hard to get right, and if you're not going to go the Rust route, I think a modern systems language should at least provide RAII-style facilities for managing complex data structures.
- platinumrad 8y agoIn my opinion, "defer" as used by Go and Zig is the way to go in more minimal (i.e. not Rust) C-like languages.
- wcrichton 8y agoIs defer commonly used for memory management in Zig? I'm not very familiar with Zig, but it seems pretty onerous to have to write defer { free(mything); } after every allocation that's supposed to be scoped. I don't think it's valid comparison to Go, since that's a garbage collected language.
- platinumrad 8y agoI've written less than 200 lines of Zig but have been kind of following its development on and off. My impression is yes, defer (and errdefer) are intended to be used for this purpose. I don't see the problem with it though.
- veebat 8y agoI just got through evaluating Zig. It's idiomatic to use defer right now but the language is also still pre-1.0 and making big changes, and that's one that might be covered in syntactical sugar at some point. Rust had a GC at one time, after all. Overall Zig feels closer in intent to C than most "better C" languages. In its current form it has a lot of attention to detail about its behavior around common edge cases. It cares about not blowing the stack and dealing with OOM, and the default behavior of numeric overflow is a crash. Stuff that allocates to the heap is stuff that the programmer is made aware of, and "defer" is one of those little additions. It's less developed w/r to higher level abstractions, with the exception of a strong metaprogramming facility and sum types.
- ori_b 8y agoTake a look at https://eigenstate.org/archive/myrddin-dev/2018-Jan/0000008.html https://eigenstate.org/archive/myrddin-dev/2018-Jan/0000008.... I think there are still going to be some changes before it stabilizes, but it's currently in master.
- adrianratnapala 8y agoSo as with `defer` in Go, the use is optional? That is, it makes non-leaky code nicer, but it does not ensure that code is non-leaky.
- steveklabnik 8y agoIf you want defer in Rust, th the scopeguard package has your back.
- AndyKelley 8y agoOne thing to note: In go, defer appends a function call to a heap allocated list that gets run at the end of a function. In zig, defer (and errdefer) do not allocate memory, and get run at scope exit.