scope() and FileConduit (D1 and Tango)

Jarrett Billingsley jarrett.billingsley at gmail.com
Fri Oct 3 15:53:56 PDT 2008


On Fri, Oct 3, 2008 at 6:44 PM, Nick Sabalausky <a at a.a> wrote:
> "Jarrett Billingsley" <jarrett.billingsley at gmail.com> wrote in message
> news:mailman.4.1223071686.3520.digitalmars-d-learn at puremagic.com...
>> On Fri, Oct 3, 2008 at 4:57 PM, Nick Sabalausky <a at 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.
>
> 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"?

It's currently harmless.  I don't know if it's defined or not.

> 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(); }
}


More information about the Digitalmars-d-learn mailing list