News for hackers. Stuff that matters.
Re:Stupid Smart Pointers in C by usrnm on Monday March 17, 2025 @09:03AM (#43388121)
That's sad. Having migrated from C++ to golang a few years ago, I find defer vastly inferior to C++ destructors. Rust did it right with its drop trait, I think it's a much better approach
Re:Stupid Smart Pointers in C by fuhsnn on Monday March 17, 2025 @09:18AM (#43388281)
The proposed C defer is scope-based unlike Go. So in the spirit of OP article, you can basically hand roll not only C++ destructor as defer {obj.dtor()} but also Rust Drop as defer {obj.notmoved() ? Drop()}
Re:Stupid Smart Pointers in C by DanielHB on Monday March 17, 2025 @09:26AM (#43388359)
What do you mean defer isn't scope based in Go?

(not super experienced Go developer)

Re:Stupid Smart Pointers in C by masklinn on Monday March 17, 2025 @09:27AM (#43388375)
In Go, defers are function scoped not block scoped.
Re:Stupid Smart Pointers in C by DanielHB on Monday March 17, 2025 @11:12AM (#43389422)
Right, that makes more sense.
Re:Stupid Smart Pointers in C by TheDong on Monday March 17, 2025 @10:06AM (#43388750)
I mean, you just write all your scopes as `(func() { })()` in go, and it works out fine.

Adding `func() {}()` scopes won't break existing code usually, though if you use 'break' or 'continue' you might have to make some changes to make it compile, like so:

https://go.dev/play/p/_Gq4QYtyMmp

see, no other issues, works exactly like you'd expect

Re:Stupid Smart Pointers in C by devnull3 on Monday March 17, 2025 @09:32AM (#43388425)
Re:Stupid Smart Pointers in C by masklinn on Monday March 17, 2025 @09:27AM (#43388371)
Although that is true, the author has expounded at lengths on the unsuitability of RAII to the C programming langage, and as a big fan of RAII the explanations were convincing.
Re:Stupid Smart Pointers in C by Dwedit on Monday March 17, 2025 @10:24AM (#43388946)
Even Zig which is extremely against "Hidden control flow" (so no operator overloading, etc.) added in the "defer" feature.
Re:Stupid Smart Pointers in C by vbezhenar on Monday March 17, 2025 @09:30AM (#43388402)
What's "drop trait" for C? There are no any traits in C.
Re:Stupid Smart Pointers in C by MathMonkeyMan on Tuesday March 18, 2025 @01:18AM (#43396021)
Parent post said Rust.

edit: TIL: Looks like the [drop trait][1] is an opt-in destructor that the Rust compiler understands.

[1]: https://doc.rust-lang.org/nomicon/dropck.html

Re:Stupid Smart Pointers in C by ynik on Tuesday March 18, 2025 @09:58AM (#43399468)
Destructors/drop have issues though:

* cannot return errors/throw exceptions * cannot take additional parameters (and thus do not play well with "access token" concepts like pyo3's `Python` token that proves the GIL was acquired -- requiring the drop implementation to re-acquire the lock just in case)

I think `defer` would be a better language construct than destructors, if it's combined with some kind of linear types that produce compiler errors if there is some code path that does not move/destroy the object.