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=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.180.76.76 with SMTP id i12mr2912431wiw.6.1368422472624; Sun, 12 May 2013 22:21:12 -0700 (PDT) Path: border1.nntp.dca3.giganews.com!border2.nntp.dca3.giganews.com!border4.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!h2no1410154wiw.1!news-out.google.com!hg5ni103141wib.1!nntp.google.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: Seeking for papers about tagged types vs access to subprograms Date: Mon, 13 May 2013 08:21:14 +0300 Organization: Tidorum Ltd Message-ID: References: <1vrhb7oc4qbob$.q02vuouyovp5$.dlg@40tude.net> <19lrzzbgm77v6.1dzpgqckptaj6.dlg@40tude.net> <1bfhq7jo34xpi.p8n2vq6yjsea.dlg@40tude.net> <12gn9wvv1gwfk.10ikfju4rzmnj.dlg@40tude.net> Mime-Version: 1.0 X-Trace: individual.net nRIxIoufvufWFA2pRe34GAsfe+XE9KQ+qpBHPm541x7D/KEMnn Cancel-Lock: sha1:jCuuii1vCd6CsA/bDm+vJM5ALqY= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:17.0) Gecko/20130328 Thunderbird/17.0.5 In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Original-Bytes: 4348 Xref: number.nntp.dca.giganews.com comp.lang.ada:181611 Date: 2013-05-13T08:21:14+03:00 List-Id: On 13-05-12 21:56 , Jeffrey Carter wrote: > On 05/12/2013 01:10 AM, Niklas Holsti wrote: >> >> You mean like this: >> >> function Open (File : in Closed_File) return Open_File; >> function Close (File : in Open_File ) return Closed_File; >> >> I agree that this is a possible approach. This would work, but at the >> cost of writing the parameter/object name twice in each call: >> >> File : File_Object; >> ... >> File := Open (File); >> File := Close (File); > > I'm not sure I see the point in having an object for a closed file, > other than the requirements of low-level languages in which such things > were 1st implemented. This "file" stuff is only an example of a protocol that can be subjected to typestate analysis. For "closed file", substitute "an object that is in its initial state". > Why not something like > > type File_Info (<>) is tagged limited private; > > function Open (Name : ...; ...) return File_Info; > function Create (Name : ...; ...) return File_Info; > > function Read (File : in out File_Info) return ...; > procedure Write (File : in out File_Info; Item : in ...); > > declare > File : File_Info := Open ("junk", ...); > begin > Data := Read (File); > ... > end; > > A File_Info must be opened or created when declared, and is closed when > it's finalized. The point of the example was to illustrate typestate analysis. For this example, with only Open - (Read/Write)* - Close, your approach collapses the example into a trivial one, where adherence to the operation protocol is ensured by the necessity to declare a File_Info object before it can be used. So it would no longer be a useful example. Your approach works in some cases, but it is problematic if you cannot initialize the object to an "active" state in its declaration, for example because the "activation" is conditional in some way, or must be delayed. The "activation" must then be done in a statement, and the declaration leaves the object in some inactive initial state. Another limitation in your approach is that the opening and closing of different files must be strictly nested: if you declare and open file A, then declare and open file B, file B must be closed before file A is closed (as long as it all happens in the same task). Such forced constraints between the states of different object is often not wanted. -- Niklas Holsti Tidorum Ltd niklas holsti tidorum fi . @ .