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.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,29e947df2e56cc40 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-07-30 11:51:27 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!bloom-beacon.mit.edu!nycmny1-snh1.gtei.net!news.gtei.net!news-out.visi.com!hermes.visi.com!newsfeed.news2me.com!newsfeed2.earthlink.net!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!stamper.news.atl.earthlink.net!harp.news.atl.earthlink.net!not-for-mail From: Richard Riehle Newsgroups: comp.lang.ada Subject: Re: What's it's name again? Date: Tue, 30 Jul 2002 11:55:54 -0700 Organization: AdaWorks Software Engineering Message-ID: <3D46E13A.10D50FDF@adaworks.com> References: Reply-To: richard@adaworks.com NNTP-Posting-Host: 3f.bb.69.d2 Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Server-Date: 30 Jul 2002 18:51:26 GMT X-Mailer: Mozilla 4.7 [en] (Win98; I) X-Accept-Language: en Xref: archiver1.google.com comp.lang.ada:27489 Date: 2002-07-30T18:51:26+00:00 List-Id: The question about completing an incomplete type in the package body has lots of interesting history. The name is, opaque type, and it comes from Modula-2. If Ada had followed the Modula-2 language design, there would be no private part in the package specification (Modula-2's Definition Module). All complete type definitions would be in the package body (Implementation Module). This design decsion created a problem for Modula-2 when trying to do separate compilations. The Modula-2 solution was to create "opaque types" that were referenced through a pointer mechanism. Modula-3 continues to follow some of the Modula-2 rules but simplifies this feature with some new language syntax. Ada's solution was to include a private part in the specification. This allowed the compiler developers enough information to do separate compilation at the specification level without resorting to opaque types. The example below, contributed by Robert Duff, is an example of creating an opaque type in Ada. This idiom is becoming more and more common in reusable components at some of our client sites. With Ada 83, opaque types were not too problematic. With the need for extensible types in Ada 95 a new problem arose: how to create extensible opaque types? There is a solution to this problem, but it requires additional levels of indirection. I am including some sample code at the end of this message for anyone who wants to play with it. Robert A Duff wrote: > "chris.danx" writes: > > > What do you call private types whose declaration is found in the > > specification but whose definition is found in the body? I remember > > reading/hearing that they have a name (or term) associated with them, > > but can't remember what it is. > > That's not quite legal. I think you mean a private type completed by an > access type, where the access type points to an incomplete type > completed in the body. Like this: > > package P is > type T is private; > private > type R; -- completed in body > type T is access R; > end P; > > package body P is > type R is > record > ... > > I call R an "stt incomplete type", and I call T an "stt access type", > named after Tucker Taft (initials "stt"), who invented them around 1982 > or so, just before Ada 83 was standardized. Jean Ichbiah called them > "Taft Amendment Types". > > > Are there any complications with moving type definitions to the body? > > (e.g. with controlled types?). > > None other than the fact that you have to have a pointer type involved. > If I need a pointer type anyway, then I use stt access types, but I > don't normally introduce *extra* pointers just to be able to use this > feature. > > Introducing extra pointers causes the obvious problems: how do you > allocate and deallocate the pointed-to things? > > > I currently have some packages which look like > > > > package some_kind_of_data_structure is > > > > type data_structure is private; > > > > private > > > > type data_structure is Ada.Finalized.Controlled with record > > ... > > end record; > > > > end some_data_structure; > > >From Richard Riehle. This code will compile, but we don't have it totally implemented in this sample. You can implement the Controlled type procedures as you wish, and don't forget to handle potential memory leaks. First we show the package body. -- Example of an Opaque Controlled Type -- Author: Richard Riehle, AdaWorks Software Engineering -- Permission to use this as you wish for any purpose at all. with Ada.Finalization; use Ada; package Opaque_Tagged_Type is type Opaque is tagged private; procedure Create (Data : in out Opaque); private type Opaque_Data; type Data_Reference is access all Opaque_Data; type Opaque is new Ada.Finalization.Controlled with record Value : Data_Reference; end record; procedure Initialize(Data : in out Opaque); procedure Finalize (Data : in out Opaque); procedure Adjust (Data : in out Opaque); end Opaque_Tagged_Type; -- ====================================================== Now we provide the stubbed-out code for the package body. package body Opaque_Tagged_Type is type Opaque_Data is new Ada.Finalization.Controlled with record X : Integer; Y : Integer; end record; procedure Initialize(Data : in out Opaque_Data); procedure Finalize (Data : in out Opaque_Data); procedure Adjust (Data : in out Opaque_Data); procedure Create(Data : in out Opaque) is Temp : Data_Reference := new Opaque_Data'(Ada.Finalization.Controlled with X => 0, Y => 0); begin Data.Value := Temp; end Create; procedure Initialize (Data : in out Opaque) is begin Data.Value.X := 0; Data.Value.Y := 0; end Initialize; procedure Finalize (Data : in out Opaque)is begin null; end Finalize; procedure Adjust (Data : in out Opaque)is begin null; end Adjust; procedure Initialize (Data : in out Opaque_Data) is begin null; end Initialize; procedure Finalize (Data : in out Opaque_Data)is begin null; end Finalize; procedure Adjust (Data : in out Opaque_Data)is begin null; end Adjust; end Opaque_Tagged_Type;