News for hackers. Stuff that matters.
Re:Myrddin Programming Language by platinumrad on Sunday June 03, 2018 @04:16PM (#17221537)
In my opinion, "defer" as used by Go and Zig is the way to go in more minimal (i.e. not Rust) C-like languages.
Re:Myrddin Programming Language by ori_b on Sunday June 03, 2018 @04:25PM (#17221600)
Take a look at

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.

Re:Myrddin Programming Language by adrianratnapala on Sunday June 03, 2018 @07:04PM (#17222951)
So 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.

Re:Myrddin Programming Language by AndyKelley on Monday June 04, 2018 @04:38AM (#17225712)
One 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.

Re:Myrddin Programming Language by wcrichton on Sunday June 03, 2018 @04:19PM (#17221556)
Is 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.

Re:Myrddin Programming Language by veebat on Sunday June 03, 2018 @06:05PM (#17222553)
I 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.

Re:Myrddin Programming Language by platinumrad on Sunday June 03, 2018 @04:28PM (#17221624)
I'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.
Re:Myrddin Programming Language by steveklabnik on Sunday June 03, 2018 @07:00PM (#17222922)
If you want defer in Rust, th the scopeguard package has your back.