Earlier quoted context omitted.
HN has some heuristics to reduce hyperbole in submissions which occasionally backfire amusingly.
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…
C++ says “We have try... finally at home”
101–110 of 152 posts
Re: C++ says “We have try... finally at home”
#102Earlier 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…
Submissions with titles that undergo this treatment should get a separate screen where both titles are proposed, and the ultimate choice belongs to the submitter.
Re: C++ says “We have try... finally at home”
#103Earlier quoted context omitted.
You'd write it like this void bar() { try (var f = foo()) { doMoreHappyPath(f); } catch(IOException ex) { handleErrors(); } } File foo() throws IOException { File f = openFile(); doHappyPath(f); if (badThing) { throw new IOException("Bad thing"); } return f; } That said, I think this is a bad practice (IMO). Generally speaking I think the opening and closing of a resource should happen at the same scope. Making it no…
In Java , I agree with you that the opening and closing of a resource should happen at the same scope. This is a reasonable rule in Java , and not following it in Java is a recipe for errors because Java isn't RAII . In C++ and Rust, that rule doesn't make sense. You can't make the mistake of forgetting to close the file. That's why I say that Java, Python and C#'s context managers aren't remotely the same. They're u…
But you can make a few mistakes that can be hard to see. For example, if you put a mutex in an object you can accidentally hold it open for longer than you expect since you've now bound the life of the mutex to the life of the object you attached it to. Or you can hold a connection to a DB or a file open for longer than you expected by merely leaking out the file handle and not promptly closing it when you are finished with it.
Trying to keep resource open and close in the same scope is an ownership thing. Even for C++ or Rust, I'd consider it not great to leak out RAII resources from out of the scope that acquired them. When you spread that sort of ownership throughout the code it becomes hard to conceptualize what the state of a program would be at any given location.
The exception is memory.
Re: C++ says “We have try... finally at home”
#104I 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…
Re: C++ says “We have try... finally at home”
#105Earlier quoted context omitted.
Lisp doesnt have much syntax to speak of. All of the DSLs use the same basic structure and are easy to read. Cpp has A LOT A of syntax: init rules, consts, references, move, copy, templates, special cases, etc. It also includes most of C, which is small but has so many basic language design mistakes that "C puzzles" is a book.
The syntax and the concepts (const, move, copy, etc) are orthogonal. You could possibly write a lisp / s-exp syntax for c++ and all it would make better would be the macros in the preprocessor. The DSL doesn't have to be hard to read if it uses unfamiliar/uncommon project specific concepts.
What i mean is that in cpp all the numerous language features are exposed through little syntax/grammar details. Whereas in Lisps syntax and grammar are primitive, and this is why macros work so well.
Re: C++ says “We have try... finally at home”
#106The submitted title is missing the salient keyword "finally" that motivates the blog post. The actual subtitle Raymond Chen wrote is: "C++ says “We have try…finally at home.”" It's a snowclone based on the meme, "Mom, can we get ? No, we have at home." : https://www.google.com/search?q=%22we+have+x+at+home%22+meme In other words, Raymond is saying... "We already have Java feature of 'finally' at home in the C++ refri…
HN has some heuristics to reduce hyperbole in submissions which occasionally backfire amusingly.
Re: C++ says “We have try... finally at home”
#107Earlier quoted context omitted.
I don't view finalizers and destructors as different concepts. The notion only matters if you actually need cleanup behavior to be deterministic rather than just eventual, or you are dealing with something like thread locals. (Historically, C# even simply called them destructors.)
> I don't view finalizers and destructors as different concepts. They are fundamentally different concepts. See Destructors, Finalizers, and Synchronization by Hans Boehm - https://dl.acm.org/doi/10.1145/604131.604153
Re: C++ says “We have try... finally at home”
#108Re: C++ says “We have try... finally at home”
#109Destructors 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?
You need to read the article again because your assertion is patently false. You can throw and handle exceptions in destructors. What you cannot do is not catch those exceptions, because as per the standard uncaught exceptions will lead the application to be immediately terminated.
Re: C++ says “We have try... finally at home”
#110> In Java, Python, JavaScript, and C# an exception thrown from a finally block overwrites the original exception, and the original exception is lost. Pet peeve of mine: all these languages got it wrong. (And C++ got it extra-wrong.) The error you want to log or report to the user is almost certainly the original exception, not the one from the finally block. The error from the finally block is probably a side effect…