Earlier quoted context omitted.
> a defer in an if-block executes instantly, so you cannot conditionally defer anymore. Of course you can, using ?: (or && and || if you prefer), just like any other case where you want an expression rather than a statement. Or simply using the non-block form of if. (Some stupid autoformatters or tech leads insert extraneous braces, but you should be avoiding those already).
The parent I was replying to was talking about Go. There is no ternary operator in Go. There are no non-block forms of if in Go. I'm not sure what your "of course" is referring to, but I guess it is unrelated.
The Defer Technical Specification: It Is Time
71–77 of 77 posts
Re: The Defer Technical Specification: It Is Time
#72Earlier quoted context omitted.
yeah, I've always just extracted the loop body into a new function as a result
I've created a lambda and called it inline to force lexically scoped defer semantics. Works fine and reads fine imo.
Given how some of the other ergonomic changes in go have gone (closures capturing loop variables, for instance), I'd support a change to lexical scoped defers if it were on the table.
Re: The Defer Technical Specification: It Is Time
#73Re: The Defer Technical Specification: It Is Time
#74I've been doing properly scoped defers in C since forever, as long as you have access to cleanup attributes and nested functions it's no big deal. https://github.com/codr7/hacktical-c/tree/main/macro
Re: The Defer Technical Specification: It Is Time
#75Earlier quoted context omitted.
The author is the project editor for the ISO C standard. (And I hardly think that analyzing speculating about the motivation for the author's chosen nickname is constructive.)
Great! I'm a programmer. And I've sure spent too much time on C++isms. > (And I hardly think that analyzing speculating about the motivation for the author's chosen nickname is constructive.) Nope! Gets right to it. This is really building C++ (but this time how I want). It adds work for every C programmer who has to check off a whole bunch of small tasks to keep a codebase living for many years.