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-Thread: 103376,437103ff8a92c0df X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!newsread.com!news-xfer.newsread.com!newspeer.monmouth.com!newsfeed.icl.net!newsfeed.fjserv.net!feed.news.tiscali.de!newsfeed01.sul.t-online.de!t-online.de!newsfeed.arcor.de!news.arcor.de!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: generics and records Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.14.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <1109532840.857126.234720@z14g2000cwz.googlegroups.com> Date: Sun, 27 Feb 2005 21:16:20 +0100 Message-ID: NNTP-Posting-Date: 27 Feb 2005 21:16:03 MET NNTP-Posting-Host: 0b8c93e6.newsread4.arcor-online.net X-Trace: DXC=MDCbg5jmE2dUWaeE\nHN0l:ejgIfPPlddjW\KbG]kaMhGSi?jHD8GO`3Geomji7SR9?0h X-Complaints-To: abuse@arcor.de Xref: g2news1.google.com comp.lang.ada:8518 Date: 2005-02-27T21:16:03+01:00 List-Id: On 27 Feb 2005 11:34:00 -0800, spambox@volja.net wrote: > in the process of learning Ada, I came to the problem of generics and > records. I want to instantiate a package with a record type: > > generic > type x is private; > package ... > private > type xx is new x; > end ... > > When I want to use it: > > type myrecord is > ... > package somename is new seeabove(myrecord); > > I don't know if this is the correct way. The problem is, how do I make > a component of myrecord available to the package? A generic package knows as much about its actual parameters as the formal parameters tell. If the formal parameter is nothing more than just "is private" then it is all what the package may know, i.e. almost nothing. If the components of the actual parameter need to be known in the package, then that has to be reflected in by the formal parameters. For example: 1. Getter / setter used to access the components generic type Record_Type is private; type Value_Type is private; function Get_A (Object : Record_Type) return Value_Type is <>; procedure Set_A (Object : in out Record_Type; A : Value_Type) is <>; package Foo is ... Instantiation: type My_Record is A : Float; end record; function Get_A (Object : My_Record) return Float; procedure Set_A (Object : in out My_Record; A : Float); package My_Foo is new Foo (My_Record, Float); 2. Inheritance from a record type with known components: type Common_Base is tagged record A : Float; -- This will be visible in Foo end record; generic type Record_Type is new Common_Base with private; package Foo is ... Instantiation: type My_Record is new Common_Base with record ... -- These new components will be inaccessible in Foo! end record; package My_Foo is new Foo (My_Record); 3. Mixing of 1 and 2. The common base is abstract and has no components. Instead of that it defines the interface (getter and setter) to access the components. type Common_Base is abstract tagged null record; function Get_A (Object : Common_Base) return Float is abstract; procedure Set_A (Object : in out Common_Base; A : Float) is abstract; Here Common_Base defines the interface of a record type with Get_A and Set_A. generic type Record_Type is new Common_Base with private; package Foo is ... Instantiation: type My_Record is new Common_Base with A : Float; end record; function Get_A (Object : My_Record) return Float; -- Override procedure Set_A (Object : in out My_Record; A : Float); package My_Foo is new Foo (My_Record); -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de