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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!aotearoa.belnet.be!news.belnet.be!feed1.news.be.easynet.net!news.skynet.be!195.238.0.222.MISMATCH!newsspl501.isp.belgacom.be!tjb!not-for-mail Date: Wed, 13 May 2009 17:54:04 +0200 From: Olivier Scalbert User-Agent: Thunderbird 2.0.0.21 (X11/20090318) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: newbie problem References: <4a0ad646$0$2854$ba620e4c@news.skynet.be> <99968fcb-2fc4-4816-87bb-f88e27143a2a@r31g2000prh.googlegroups.com> In-Reply-To: <99968fcb-2fc4-4816-87bb-f88e27143a2a@r31g2000prh.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <4a0aed1a$0$2865$ba620e4c@news.skynet.be> Organization: -= Belgacom Usenet Service =- NNTP-Posting-Host: 772d35a1.news.skynet.be X-Trace: 1242230042 news.skynet.be 2865 87.65.200.8:60234 X-Complaints-To: usenet-abuse@skynet.be Xref: g2news2.google.com comp.lang.ada:5819 Date: 2009-05-13T17:54:04+02:00 List-Id: Adam Beneschan wrote: > You could make it an access to a File_Type, as Martin mentioned. But > you may also want to make *your* type limited. Making it limited > would prevent the program from copying objects of that type, and that > could be exactly what you want. > > A record that represents information about a file that's currently > being used can be a natural candidate for a "limited" type. Suppose, > hypothetically, that you're writing your own file I/O package, and not > using the standard Ada libraries. You'll probably have some sort of > record somewhere to store information about the current file position, > perhaps some cached data from the file, etc.: > > type File_Info is record > Curr_Position : Long_Integer; > Buffer : Byte_Array(1..Buf_Size); > ... > end record; > > Most likely, this would be in the private part of your package. > Suppose you declare this without using "limited": > > type File_Info is private; > > Now some other part of the program that WITH's your package can say > > F1 : File_Info; > F2 : File_Info; > > and then, after F1 has been created and some I/O done using it: > > F2 := F1; > > This could screw up your whole file package's operations. Suppose the > program later does some operations on F1, and it modifies the > Curr_Position and fills some data in the Buffer. This will change the > information in F1, but not the information in F2. If the program then > calls an I/O procedure using F2, the package will go bonkers, because > F2 doesn't have the correct file position or the correct buffered > data, and it's very likely that something bad will happen, such as > data being flushed to the wrong position of the file and turning your > file into junk. > > A solution here is to prevent assignment from happening, by declaring > the type File_Info as "limited private". This would make the above > assignment statement illegal, as well as other Ada constructs that > could result in two copies of the record being created. > > I'm not saying that this is definitely what you want. But you should > consider it. If you do, you'd change the first declaration to > > type Als_File_T is limited private; > > You'll also have to look into Ada 2005's "extended return" statement > in order to write the body of Als_Create. > > Anyway, that's one thing I think you should consider. If that isn't > what you want---i.e. if Als_File_T is supposed to represent a > "reference" or "handle" to a file rather than the file itself---or if > this is all too confusing, then access types will work. It's a design > decision. > > -- Adam > Thanks Adam for the very clear explanation ! Olivier.