comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adambeneschan@gmail.com>
Subject: Re: Curiosity in generic package instantiation
Date: Tue, 19 Aug 2014 16:16:48 -0700 (PDT)
Date: 2014-08-19T16:16:48-07:00	[thread overview]
Message-ID: <f9998a20-46fd-492a-b8a3-02105f728e82@googlegroups.com> (raw)
In-Reply-To: <lt0itn$1g5$1@dont-email.me>

On Tuesday, August 19, 2014 3:26:01 PM UTC-7, GianLuigi Piacentini wrote:
> Hi all,
> 
> pardon if using not the exact terminology (too old to learn ADA intricacies, 
> whish had done it back in the '80-'90 when I was active programmer, but then 
> in Italy buzzword was c/c++ m$oft version, and VB), I noticed the following 
> excerpt in a previous thread:
> 
> ...
> 
> procedure Test_List is
>     type Example_Type is record
>       A :Integer := 0;
>       B: Integer := 0;
>     end record;
> 
> ...
> 
>   declare
>     Data : Example_Type;
>     package Example_Pkg is new
> Ada.Containers.Doubly_Linked_Lists(Example_Type);
>     List : Example_Pkg.List;
> 
> if I understand well, this means that the Ada.Containers.Doubly_Linked_Lists 
> package is instantiated using Example_Type, and the instantiated list name 
> is Example_Pkg.List.
> 
> Is this done by text substitution at source/intermediate code level 
> (something like the C preprocessor), albeit in an user-transparent (=better) 
> way, or is it obtained by some property of the list elements, able to store 
> whatever is thrown at them, even elements of different type in the same 
> list?
> 
> My 1st guess looking at the code is sort of compiler-integrated 
> preprocessing.
> 
> Just curious.

An implementation can do either.  Suppose you instantiate Ada.Containers.Doubly_Linked_Lists with multiple types in the same program:

    package Example_Pkg is new Ada.Containers.Doubly_Linked_List(Example_Type);
    package Another_Pkg is new Ada.Containers.Doubly_Linked_List(Another_Type);
    package A_Third_Pkg is new Ada.Containers.Doubly_Linked_List(Type_3);

If you compile with GNAT, you'll get three copies of the Doubly_Linked_List code, with each copy set up to work on a different type.  If you compile with Janus Ada, you will get only one copy of the code.  I'm not entirely sure what the code looks like, but probably when the subprograms in the instantiation are called, they will be given a parameter that contains some information needed about each type, such as the size of the type, so that the code in the instantiation will know what to do when copying objects of the type around.  I'm assuming this approach takes less code space but performs a little slower.

The language is designed to support both approaches, and the designers are careful to make sure that both approaches are possible when new language features are added.

The language rules about generic make it so that instantiating a generic is *almost* the same as text substitution, but not quite.  The names and operators in generic code normally mean what they mean when the generic is *compiled*, not when it's instantiated (with some exceptions).  So if you say

    generic
        type FLOAT_TYPE is digits <>;  -- generic formal floating-point type
    package Some_Package is ...

and in the body, you use X + Y on two FLOAT_TYPE's, you will always get the standard meaning of "+" even if you redefine "+" to mean something else on some user-defined floating-point type (a bad idea anyway) and instantiate your package with that type.  If it were a straight text substitution, you'd get the redefined "+", but that isn't how Ada works.  The actual rules are complicated and there are some nuances, though.  But it shouldn't be an issue, until you start writing more complex generics.

                                -- Adam


  reply	other threads:[~2014-08-19 23:16 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-19 22:26 Curiosity in generic package instantiation GianLuigi Piacentini
2014-08-19 23:16 ` Adam Beneschan [this message]
2014-08-20  8:56   ` Mark Lorenzen
2014-08-21 22:32     ` Randy Brukardt
2014-08-21 22:39   ` Randy Brukardt
2014-08-20  7:18 ` Björn Lundin
replies disabled

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