Live data from Hacker News

C++ says “We have try... finally at home”

devblogs.microsoft.com

51–60 of 152 posts

Re: C++ says “We have try... finally at home”

#51
post #9

Earlier quoted context omitted.

Yeah it's a huge mistake IMO. I see it fucking up titles so frequently, and it flies in the face of the "do not editorialise titles" rule: [...] please use the original title, unless it is misleading or linkbait; don't editorialize. It is much worse, I think, to regularly drastically change the meaning of a title automatically until a moderator happens to notice to change it back, than to allow the occasional somewha…

You can always contact hn@ycombinator.com to point out errors of this nature and have it corrected by one of the mods.

A better approach would be to not so aggressively modify headlines.

Relying on somebody to detect the error, email the mods (significant friction), and then hope the mods act (after discussion has already been skewed) is not really a great solution.

Re: C++ says “We have try... finally at home”

#52

Destructors are vastly superior to the finally keyword because they only require us to remember a single time to release resources (in the destructor) as opposed to every finally clause. For example, a file always closes itself when it goes out of scope instead of having to be explicitly closed by the person who opened the file. Syntax is also less cluttered with less indentation, especially when multiple objects are…

The entire point of the article is that you cannot throw from a destructor. Now how do you signal that closing/writing the file in the destructor failed?

Re: C++ says “We have try... finally at home”

#53
post #33

Earlier quoted context omitted.

I don't know Rust but, can this `defer` evaluate after the `return` statement is evaluated like in Swift? Because in Swift you can do this: func atomic_get_and_inc() -> Int { sem.wait() defer { value += 1 sem.signal() } return value }

EDIT: I don’t think you can actually put a return in a defer, I may have misremembered, it’s been several years. Disregard this comment chain. It gets even better in swift, because you can put the return statement in the defer, creating a sort of named return value: func getInt() -> Int { let i: Int // declared but not // defined yet! defer { return i } // all code paths must define i // exactly once, or it’s a compi…

Huh, I didn't know about `return` in `defer`, but is it really useful?

Re: C++ says “We have try... finally at home”

#54
post #48
post #3

I like how Swift solved this: there's a more universal `defer { ... }` block that's executed at the end of a given scope no matter what, and after the `return` statement is evaluated if it's a function scope. As such it has multiple uses, not just for `try ... finally`.

I think Swift’s defer ( https://docs.swift.org/swift-book/documentation/the-swift-pr... ) was inspired by/copied from go ( https://go.dev/tour/flowcontrol/12 ), but they may have taken it from an even earlier language that I’m not aware of. Defer has two advantages over try…finally: firstly, it doesn’t introduce a nesting level. Secondly, if you write foo defer revert_foo , when scanning the code, it’s easier to veri…

Yeah, it's especially handy in UI code where you can have asynchronous operations but want to have a clear start/end indication in the UI:

    busy = true
    Task {
        defer { busy = false }
        // do async stuff, possibly throwing exceptions and whatnot
    }

Re: C++ says “We have try... finally at home”

#55
post #36
post #32

Earlier quoted context omitted.

It's because DSLs there reduce cognitive load for the reader rather than add up to it.

Well-designed abstractions do that in every language. And badly designed ones do the opposite, again in all languages. There's nothing special about Lisp here

Sure but it's you who singled out Lisp here. The whole point of DSL is designing a purpose formalism that makes a particular problem easy to reason about. That's hardly a parallel to ever-growing vocabulary of standard C++.

Re: C++ says “We have try... finally at home”

#56
post #33
post #6

Earlier quoted context omitted.

I was contemplating what it would look like to provide this with a macro in Rust, and of course someone has already done it. It's syntactic sugar for the destructor/RAII approach. https://docs.rs/defer-rs/latest/defer_rs/

I don't know Rust but, can this `defer` evaluate after the `return` statement is evaluated like in Swift? Because in Swift you can do this: func atomic_get_and_inc() -> Int { sem.wait() defer { value += 1 sem.signal() } return value }

It's easy to demonstrate that destructors run after evaluating `return` in Rust:

    struct PrintOnDrop;
    
    impl Drop for PrintOnDrop {
        fn drop(&mut self) {
            println!("dropped");
        }
    }
    
    fn main() {
        let p = PrintOnDrop;
        return println!("returning");
    }
But the idea of altering the return value of a function from within a `defer` block after a `return` is evaluated is zany. Please never do that, in any language.

Re: C++ says “We have try... finally at home”

#57
This is a good “how C++ does it” explanation, but I think it’s more accurate to say destructors implement finally-style cleanup in C++, not that they are finally. finally is about operation-scoped cleanup; destructors are about ownership. C++ just happens to use the same tool for both.

Re: C++ says “We have try... finally at home”

#58

I always wonder whether C++ syntax ever becomes readable when you sink more time into it, and if so - how much brain rewiring we would observe on a functional MRI.

In my opinion, C++ syntax is pretty readable. Of course there are codebases that are difficult to read (heavily abstracted, templated codebases especially), but it's not really that different compared to most other languages. But this exists in most languages, even C can be as bad with use of macros. By far the worst in this aspect has been Scala, where every codebase seems to use a completely different dialect of th…

Scala is a meta language. It's really a language construction toolkit in a box.

Re: C++ says “We have try... finally at home”

#59

Destructors are vastly superior to the finally keyword because they only require us to remember a single time to release resources (in the destructor) as opposed to every finally clause. For example, a file always closes itself when it goes out of scope instead of having to be explicitly closed by the person who opened the file. Syntax is also less cluttered with less indentation, especially when multiple objects are…

A writable file closing itself when it goes out of scope is usually not great, since errors can occur when closing the file, especially when using networked file systems. https://github.com/isocpp/CppCoreGuidelines/issues/2203

Any fallible cleanup function is awkward, regardless of error handling mechanism.

Re: C++ says “We have try... finally at home”

#60

Destructors are vastly superior to the finally keyword because they only require us to remember a single time to release resources (in the destructor) as opposed to every finally clause. For example, a file always closes itself when it goes out of scope instead of having to be explicitly closed by the person who opened the file. Syntax is also less cluttered with less indentation, especially when multiple objects are…

The entire point of the article is that you cannot throw from a destructor. Now how do you signal that closing/writing the file in the destructor failed?

That tastes like leftover casserole instead of pizza.
Post reply on HN