www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - `finally` is redundant?

reply Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
The page http://dlang.org/exception-safe.html says:

"It's try-finally that becomes redundant."

IIUC this is because we have scope(exit).

Does this mean that `finally` should eventually be removed from the 
language?

-- 
Shriramana Sharma, Penguin #395953
Nov 20 2015
next sibling parent Chris Wright <dhasenan gmail.com> writes:
On Sat, 21 Nov 2015 11:15:22 +0530, Shriramana Sharma wrote:

 The page http://dlang.org/exception-safe.html says:
 
 "It's try-finally that becomes redundant."
 
 IIUC this is because we have scope(exit).
 
 Does this mean that `finally` should eventually be removed from the
 language?
It's not necessarily the case that a feature should be removed from the language merely because it could be replicated with other features. We have structs, for instance, and we could use them to implement classes (albeit with a fair bit of pain). In order to remove "finally" from the language, someone would have to sit down and analyze how much code it would break, how to fix it, and whether the fix is more readable and intuitive than the current version. Then they'd have to champion this change to the language designers -- primarily Walter. Nobody has chosen to do that as yet, so removing "finally" isn't on the table. The main thing that try/finally gives you that scope guards generally don't is that the finally block executes immediately after the try block (or except block if it's there and an exception was thrown). For example: --- auto tx = db.beginTransaction; try { exportToJson(db.select("* FROM Users")); } finally { tx.close; } db.dropTable("Users"); --- Here, we want to ensure that we export a consistent snapshot of the Users table, so we use a transaction. And let's say our hypothetical database will refuse to drop the table if there's an open transaction. How would I replicate this with scope guards? --- { auto tx = db.beginTransaction; scope (exit) tx.close; exportToJson(...); } db.dropTable("Users"); --- Is this version better? Well, during code review, I can more quickly see that the transaction won't be left open. But every other time I'm reading the method, I'm dealing with the fact that the statements shown are executed in a different order than I read them in. Plus I have to spend extra thought on scope in order to determine what the function does. (That's part of the reason it's nice to forbid shadowing declarations. When I'm reading something that compiles, I can pretend that variables are in scope from declaration until the end of the function, which means less worrying about scope.) So it's a bit of a toss-up at best. I'd expect a conservative decision if you pushed a D Improvement Proposal far enough to get a ruling.
Nov 21 2015
prev sibling next sibling parent reply Jonathan M Davis via Digitalmars-d-learn writes:
On Saturday, November 21, 2015 11:15:22 Shriramana Sharma via
Digitalmars-d-learn wrote:
 The page http://dlang.org/exception-safe.html says:

 "It's try-finally that becomes redundant."

 IIUC this is because we have scope(exit).

 Does this mean that `finally` should eventually be removed from the
 language?
finally isn't going anywhere, and there are cases where it still makes sense. Also, when scope(exit) is used, the code is "lowered" to be use finally (i.e. the code is transformed by the compiler from one construct to another). So, currently, the only reason that scope(exit) even works is because we have finally. Getting rid of finally would mean reimplementing scope(exit) differently, and there really isn't any reason to get rid of it anyway. Just because scope(exit) is usually better doesn't mean that finally is never appropriate. - Jonathan M Davis
Nov 21 2015
parent reply Chris Wright <dhasenan gmail.com> writes:
On Sat, 21 Nov 2015 16:10:45 -0800, Jonathan M Davis via
Digitalmars-d-learn wrote:

 Getting rid of finally would mean
 reimplementing scope(exit) differently
Well, we could keep try/finally as a concept inside the compiler but change the parser so it rejects finally blocks.
Nov 21 2015
parent Jonathan M Davis via Digitalmars-d-learn writes:
On Sunday, November 22, 2015 02:00:46 Chris Wright via Digitalmars-d-learn
wrote:
 On Sat, 21 Nov 2015 16:10:45 -0800, Jonathan M Davis via
 Digitalmars-d-learn wrote:

 Getting rid of finally would mean
 reimplementing scope(exit) differently
Well, we could keep try/finally as a concept inside the compiler but change the parser so it rejects finally blocks.
I expect that that would complicate things a bit, but regardless, why would we do that? There's nothing wrong with finally. scope(exit) makes it less useful, but that doesn't mean that it's useless or that we should get rid of it. - Jonathan M Davis
Nov 21 2015
prev sibling parent Meta <jared771 gmail.com> writes:
On Saturday, 21 November 2015 at 05:45:25 UTC, Shriramana Sharma 
wrote:
 The page http://dlang.org/exception-safe.html says:

 "It's try-finally that becomes redundant."

 IIUC this is because we have scope(exit).

 Does this mean that `finally` should eventually be removed from 
 the language?
The most important reason not to do this is that it makes it easier to port Java, C#, Python, Ruby, etc. to D.
Nov 21 2015