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()}
[ Parent
| on HN ]
What do you mean defer isn't scope based in Go?
(not super experienced Go developer)
In Go, defers are function scoped not block scoped.
Right, that makes more sense.
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
mentioned in the article at [1]
[1] https://thephd.dev/c2y-the-defer-technical-specification-its...