Live data from Hacker News

The Defer Technical Specification: It Is Time

thephd.dev

71–77 of 77 posts

Re: The Defer Technical Specification: It Is Time

#71
post #64

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.

I figured we were talking about C since that's the main topic. In any case short-circuit && and || still work in Go AIUI, so my point stands.

Re: The Defer Technical Specification: It Is Time

#72
post #29

Earlier 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.

I've definitely done that as well. I generally don't have a problem with it, but it makes for an extra conversation the first time someone sees the pattern.

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

#74
post #3

I'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

Please don’t use nested functions; they’re a security nightmare.

Re: The Defer Technical Specification: It Is Time

#75
post #25
post #20

Earlier 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.

Guys is it C++ when I don’t accidentally leak memory

Re: The Defer Technical Specification: It Is Time

#76
post #3

I'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

Please don’t use nested functions; they’re a security nightmare.

Curious: why?
Post reply on HN