4 ms·
I love the idea of having a defer statement in general for C, but given the complexity of C scopes it's a little hard to wrangle (switch statements in particula
by andrewla 2y ago
I love the idea of having a defer statement in general for C, but given the complexity of C scopes it's a little hard to wrangle (switch statements in particular break a lot of assumptions about how scopes should work).
I would prefer a directly scoped syntax similar for a for statement, something like
defer (void * p = malloc(17); free(p)) {
...
}
This gets more cumbersome as you have more such scopes in a function, but it gives a sane bounding. You can sort of do this now with a properly constructed for loop so that it cleans up on regular exit from the loop, but it can't handle exception exits (returns, breaks, and god forbid goto or longjmp).
- gustedt 2y agoThis would be much more restrictive that what is proposed, because usually you would add `defer` statements as you go in a block, when you allocate more resources for example. Also usually you should be able to have several actions, defer { one; two; } And the bounding in the proposed feature is sane, I think, it is the surrounding compound statement (AKA `{ ... }` block).