comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: generics and records
Date: Sun, 27 Feb 2005 21:16:20 +0100
Date: 2005-02-27T21:16:03+01:00	[thread overview]
Message-ID: <ioovgn6jmqpm.n4dftfok0685$.dlg@40tude.net> (raw)
In-Reply-To: 1109532840.857126.234720@z14g2000cwz.googlegroups.com

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



  reply	other threads:[~2005-02-27 20:16 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-02-27 19:34 generics and records spambox
2005-02-27 20:16 ` Dmitry A. Kazakov [this message]
2005-02-27 21:31   ` spambox
2005-02-28  8:47     ` Dmitry A. Kazakov
2005-02-28  9:00       ` spambox
2005-02-28 10:07         ` Dmitry A. Kazakov
2005-02-28  6:02 ` Jeffrey Carter
2005-03-01 11:21 ` Martin Krischik
2005-03-01 19:47   ` Georg Bauhaus
2005-03-01 20:17     ` Dmitry A. Kazakov
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox