digitalmars.D.learn - scope() and FileConduit (D1 and Tango)
- Nick Sabalausky (10/10) Oct 03 2008 I'd like some clarification on the way scope() works, and also Tango's
- Bill Baxter (5/15) Oct 03 2008 You might be interested in the siblings of scope(exit):
- Jarrett Billingsley (4/14) Oct 03 2008 Yes, the ctor throws an exception. No, file.close is never called.
- Nick Sabalausky (12/29) Oct 03 2008 Thanks.
- Jarrett Billingsley (9/39) Oct 03 2008 It's pretty simple to derive it yourself, but it does seem like
- Steven Schveighoffer (7/37) Oct 04 2008 If you use FileConduit the way you are saying above, and the ctor throws...
- Bill Baxter (14/31) Oct 03 2008 Oh, ok I see what the issue is now. I think this will do the right thin...
- Jarrett Billingsley (6/34) Oct 03 2008 That's pretty much unnecessary, since in this code:
- Bill Baxter (4/40) Oct 03 2008 Oh yeh. Good point.
I'd like some clarification on the way scope() works, and also Tango's FileConduit. In the following function: void load(char[] infilename) { auto file = new FileConduit(infilename); scope(exit) file.close(); // Load data } What happens if infilename doesn't exist? Does FileConduit's constructor throw an exception? If so, file.close() isn't called, is it?
Oct 03 2008
You might be interested in the siblings of scope(exit): scope(success) and scope(failure). In particular, scope(success) file.close(); is probably what you're after here. --bb On Sat, Oct 4, 2008 at 5:57 AM, Nick Sabalausky <a a.a> wrote:I'd like some clarification on the way scope() works, and also Tango's FileConduit. In the following function: void load(char[] infilename) { auto file = new FileConduit(infilename); scope(exit) file.close(); // Load data } What happens if infilename doesn't exist? Does FileConduit's constructor throw an exception? If so, file.close() isn't called, is it?
Oct 03 2008
On Fri, Oct 3, 2008 at 4:57 PM, Nick Sabalausky <a a.a> wrote:I'd like some clarification on the way scope() works, and also Tango's FileConduit. In the following function: void load(char[] infilename) { auto file = new FileConduit(infilename); scope(exit) file.close(); // Load data } What happens if infilename doesn't exist? Does FileConduit's constructor throw an exception? If so, file.close() isn't called, is it?Yes, the ctor throws an exception. No, file.close is never called. scope statements are only executed if execution reaches them successfully.
Oct 03 2008
"Jarrett Billingsley" <jarrett.billingsley gmail.com> wrote in message news:mailman.4.1223071686.3520.digitalmars-d-learn puremagic.com...On Fri, Oct 3, 2008 at 4:57 PM, Nick Sabalausky <a a.a> wrote:Thanks. A minor followup question (more of a curiosity, really): If .close() is called on a conduit that is already closed, is this silently accepted or is there a "Tried to close a non-open conduit" exception? And is that behavior a defined part of the API, or just sort of "That's what it does right now, but it's not officially guaranteed to do that"? Also, it looks like neither FileConduit nor its base classes have a destructor, which suggests to me I can't use RTTI and replace both lines in the code sample above with "scope file = new FileConduit(infilename);". Any particular reason for this, or is it a possible future enhancement?I'd like some clarification on the way scope() works, and also Tango's FileConduit. In the following function: void load(char[] infilename) { auto file = new FileConduit(infilename); scope(exit) file.close(); // Load data } What happens if infilename doesn't exist? Does FileConduit's constructor throw an exception? If so, file.close() isn't called, is it?Yes, the ctor throws an exception. No, file.close is never called. scope statements are only executed if execution reaches them successfully.
Oct 03 2008
On Fri, Oct 3, 2008 at 6:44 PM, Nick Sabalausky <a a.a> wrote:"Jarrett Billingsley" <jarrett.billingsley gmail.com> wrote in message news:mailman.4.1223071686.3520.digitalmars-d-learn puremagic.com...It's currently harmless. I don't know if it's defined or not.On Fri, Oct 3, 2008 at 4:57 PM, Nick Sabalausky <a a.a> wrote:Thanks. A minor followup question (more of a curiosity, really): If .close() is called on a conduit that is already closed, is this silently accepted or is there a "Tried to close a non-open conduit" exception? And is that behavior a defined part of the API, or just sort of "That's what it does right now, but it's not officially guaranteed to do that"?I'd like some clarification on the way scope() works, and also Tango's FileConduit. In the following function: void load(char[] infilename) { auto file = new FileConduit(infilename); scope(exit) file.close(); // Load data } What happens if infilename doesn't exist? Does FileConduit's constructor throw an exception? If so, file.close() isn't called, is it?Yes, the ctor throws an exception. No, file.close is never called. scope statements are only executed if execution reaches them successfully.Also, it looks like neither FileConduit nor its base classes have a destructor, which suggests to me I can't use RTTI and replace both lines in the code sample above with "scope file = new FileConduit(infilename);". Any particular reason for this, or is it a possible future enhancement?It's pretty simple to derive it yourself, but it does seem like something that should be there. scope class AutocloseFile : FileConduit { this(char[] name) { super(name); } ~this() { close(); } }
Oct 03 2008
"Nick Sabalausky" wrote"Jarrett Billingsley" <jarrett.billingsley gmail.com> wrote in message news:mailman.4.1223071686.3520.digitalmars-d-learn puremagic.com...If you use FileConduit the way you are saying above, and the ctor throws an exception, then the variable is null, so you would get a null-pointer exception first.On Fri, Oct 3, 2008 at 4:57 PM, Nick Sabalausky <a a.a> wrote:Thanks. A minor followup question (more of a curiosity, really): If .close() is called on a conduit that is already closed, is this silently accepted or is there a "Tried to close a non-open conduit" exception? And is that behavior a defined part of the API, or just sort of "That's what it does right now, but it's not officially guaranteed to do that"?I'd like some clarification on the way scope() works, and also Tango's FileConduit. In the following function: void load(char[] infilename) { auto file = new FileConduit(infilename); scope(exit) file.close(); // Load data } What happens if infilename doesn't exist? Does FileConduit's constructor throw an exception? If so, file.close() isn't called, is it?Yes, the ctor throws an exception. No, file.close is never called. scope statements are only executed if execution reaches them successfully.Also, it looks like neither FileConduit nor its base classes have a destructor, which suggests to me I can't use RTTI and replace both lines in the code sample above with "scope file = new FileConduit(infilename);". Any particular reason for this, or is it a possible future enhancement?I'll let my previous discussion with Kris speak to that issue: http://www.dsource.org/projects/tango/forums/topic/260 -Steve
Oct 04 2008
On Sat, Oct 4, 2008 at 7:08 AM, Jarrett Billingsley <jarrett.billingsley gmail.com> wrote:On Fri, Oct 3, 2008 at 4:57 PM, Nick Sabalausky <a a.a> wrote:Oh, ok I see what the issue is now. I think this will do the right thing: void load(char[] infilename) { FileConduit file; scope(exit) { if (file) file.close(); } file = new FileConduit(infilename); // Load data } You could use "scope(success) file.close();" there instead, but that's going to be wrong if an exception is thrown down in the "//Load data" part. --bbI'd like some clarification on the way scope() works, and also Tango's FileConduit. In the following function: void load(char[] infilename) { auto file = new FileConduit(infilename); scope(exit) file.close(); // Load data } What happens if infilename doesn't exist? Does FileConduit's constructor throw an exception? If so, file.close() isn't called, is it?Yes, the ctor throws an exception. No, file.close is never called. scope statements are only executed if execution reaches them successfully.
Oct 03 2008
On Fri, Oct 3, 2008 at 8:58 PM, Bill Baxter <wbaxter gmail.com> wrote:On Sat, Oct 4, 2008 at 7:08 AM, Jarrett Billingsley <jarrett.billingsley gmail.com> wrote:That's pretty much unnecessary, since in this code: auto file = new FileConduit(infilename); scope(exit) file.close(); if FileConduit's constructor fails, there is no file to close, so there's no need to check for that.On Fri, Oct 3, 2008 at 4:57 PM, Nick Sabalausky <a a.a> wrote:Oh, ok I see what the issue is now. I think this will do the right thing: void load(char[] infilename) { FileConduit file; scope(exit) { if (file) file.close(); } file = new FileConduit(infilename); // Load data }I'd like some clarification on the way scope() works, and also Tango's FileConduit. In the following function: void load(char[] infilename) { auto file = new FileConduit(infilename); scope(exit) file.close(); // Load data } What happens if infilename doesn't exist? Does FileConduit's constructor throw an exception? If so, file.close() isn't called, is it?Yes, the ctor throws an exception. No, file.close is never called. scope statements are only executed if execution reaches them successfully.
Oct 03 2008
On Sat, Oct 4, 2008 at 10:11 AM, Jarrett Billingsley <jarrett.billingsley gmail.com> wrote:On Fri, Oct 3, 2008 at 8:58 PM, Bill Baxter <wbaxter gmail.com> wrote:Oh yeh. Good point. --bbOn Sat, Oct 4, 2008 at 7:08 AM, Jarrett Billingsley <jarrett.billingsley gmail.com> wrote:That's pretty much unnecessary, since in this code: auto file = new FileConduit(infilename); scope(exit) file.close(); if FileConduit's constructor fails, there is no file to close, so there's no need to check for that.On Fri, Oct 3, 2008 at 4:57 PM, Nick Sabalausky <a a.a> wrote:Oh, ok I see what the issue is now. I think this will do the right thing: void load(char[] infilename) { FileConduit file; scope(exit) { if (file) file.close(); } file = new FileConduit(infilename); // Load data }I'd like some clarification on the way scope() works, and also Tango's FileConduit. In the following function: void load(char[] infilename) { auto file = new FileConduit(infilename); scope(exit) file.close(); // Load data } What happens if infilename doesn't exist? Does FileConduit's constructor throw an exception? If so, file.close() isn't called, is it?Yes, the ctor throws an exception. No, file.close is never called. scope statements are only executed if execution reaches them successfully.
Oct 03 2008