comp.lang.ada
 help / color / mirror / Atom feed
From: Maciej Sobczak <see.my.homepage@gmail.com>
Subject: Re: Files and controlled types
Date: Mon, 10 Mar 2008 14:45:46 -0700 (PDT)
Date: 2008-03-10T14:45:46-07:00	[thread overview]
Message-ID: <88907fc8-4567-401b-b327-884650c1063d@p25g2000hsf.googlegroups.com> (raw)
In-Reply-To: 240c1667-3489-4094-9a77-e1284defa88e@s8g2000prg.googlegroups.com

On 10 Mar, 16:37, Adam Beneschan <a...@irvine.com> wrote:

> I'm not so sure.  What happens here?
>
>     procedure New_File (I : in Integer) is
>        Name : String := "temp/" & Integer'Image (I) & ".txt";
>        F : Ada.Text_IO.File_Type;
>     begin
>        Ada.Text_IO.Put_Line ("Opening " & Name);
>        Ada.Text_IO.Create (F, Ada.Text_IO.Out_File, Name);
>        Ada.Text_IO.Set_Output (F);
>     end New_File;
>
>     procedure Proc is
>     begin
>        New_File (-17);
>        Ada.Text_IO.Put_Line ("This is written to default output");
>     end Proc;

I will describe what I expect it to do (instead of guessing around the
AARM).

The F object in New_File, when going out of scope, is finalized and it
is when it is discovered that the file reference was "leaked" outside.
The exception is raised at this point.

Now consider the following extension to the library and the change in
behavior:

Ada.Text_IO.Set_Output (Ada.Text_IO.Release (F));

where Release is a hypothetical function that removes the ownership
from F and is used *exactly* to transfer the resource to other
managing object. Then, the F is finalized but this has no effect (F is
no longer an owner of the resource).
Note that Release can return a type that is different from File_Type
to actually enforce this protocol at compile time (no way to pass F
directly to Set_Output).

> The bottom line is that things just aren't so simple, and it's not
> obvious whether a File_Type "should" cause a Close when the File_Type
> is finalized.

From my perspective, this part of the library is half-baked.
Note also that ownership transfer is a very rarely used operation when
compared to regular file usage. I believe that language should make
that more frequent use case more intuitive and idiot-proof at the cost
of making the less frequent use cases more verbose.

--
Maciej Sobczak * www.msobczak.com * www.inspirel.com



  reply	other threads:[~2008-03-10 21:45 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-08 21:22 Files and controlled types Maciej Sobczak
2008-03-08 21:56 ` gpriv
2008-03-09  3:12 ` Jeffrey R. Carter
2008-03-09 13:27   ` Maciej Sobczak
2008-03-10 15:37     ` Adam Beneschan
2008-03-10 21:45       ` Maciej Sobczak [this message]
2008-03-09 16:39   ` gpriv
2008-03-10 15:28 ` Eric Hughes
2008-03-10 22:45   ` Robert A Duff
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox