Remix.run Logo
kbr- 4 days ago

> It’s a simple keyword, but it singlehandedly eliminates the need for any kind of RAII.

What if you want to put a resource object (which needs a cleanup on destruction) into a vector then give up ownership of the vector to someone?

I write code in go now after moving from C++ and God do I miss destructors. Saying that defer eliminates need for RAII triggers me so much

estebank 4 days ago | parent [-]

There's a school of thought that correctly states that in that case it is very easy to cause expensive drop behavior to be ran for each element in the vector where a faster batch approach could instead be done, which is doable if not encouraged with defer, so it should be prioritized to push people towards that.

I do not subscribe to that idea because with RAII you can still have batched drops, the only difference between the two defaults is that with defer the failure mode is leaks, while with RAII the failure mode is more code than you'd otherwise would have.