(Un)portable defer in C
15 points by janerik
15 points by janerik
I always liked the old for-loop trick, which occupies a similar semantic space and is portable (though of course not the Intended Use and is trying to make C something it isn’t).
for (FILE *foo = fopen(“foo.txt”, “r”); foo; fclose(foo), foo = NULL) {
…
}
Neat. Defer in Zig has been something I’m gotten super used to and it makes things a lot easier.
I did use both "defer" and GCC's nested functions in a bounding volume hierarchy builder (this one). I felt really clever. Once was enough and I never had to think about it again.
Thanks, but I'll continue using C++ and its destructors (Or Rust) ;-)
I don't get when people make comments that add nothing of value to a discussion on a public thread. Your comment kinda feels inflammatory (kind of a ragebait in a way).
This sort of banter is fine on a group chat but feels out of place here no?
Sorry about that, I admit this was inflammatory, I apologize.
The point is that some people often prefer C to other languages like C++ or Rust, because they say C is more "simple". But then you see some clever complicated hacks like these to emulate feature that are built-in in other languages.
No worries, I don't think you were doing this in bad faith or anything. It's just that this particular argument (and its counter-arguments) have been paraded around the site for years now and folks (on both sides of the debate) are kinda tired of the whole thing, and have more or less silently agreed to not rehash it. It's easy to assume that your comment was intended as ragebait (not that I do, just providing some context)