From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,ee2e271b166c2587 X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!postnews.google.com!p25g2000hsf.googlegroups.com!not-for-mail From: Maciej Sobczak Newsgroups: comp.lang.ada Subject: Re: Files and controlled types Date: Mon, 10 Mar 2008 14:45:46 -0700 (PDT) Organization: http://groups.google.com Message-ID: <88907fc8-4567-401b-b327-884650c1063d@p25g2000hsf.googlegroups.com> References: <3bd96475-530b-46a7-9b07-11ea49105b2c@e31g2000hse.googlegroups.com> <240c1667-3489-4094-9a77-e1284defa88e@s8g2000prg.googlegroups.com> NNTP-Posting-Host: 85.1.248.9 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1205185546 26837 127.0.0.1 (10 Mar 2008 21:45:46 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 10 Mar 2008 21:45:46 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: p25g2000hsf.googlegroups.com; posting-host=85.1.248.9; posting-account=bMuEOQoAAACUUr_ghL3RBIi5neBZ5w_S User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:20281 Date: 2008-03-10T14:45:46-07:00 List-Id: On 10 Mar, 16:37, Adam Beneschan 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