comp.lang.ada
 help / color / mirror / Atom feed
From: eachus@spectre.mitre.org (Robert I. Eachus)
Subject: Re: some questions re. Ada/GNAT from a C++/GCC user
Date: 1996/03/29
Date: 1996-03-29T00:00:00+00:00	[thread overview]
Message-ID: <EACHUS.96Mar29110704@spectre.mitre.org> (raw)
In-Reply-To: leschkes.828053356@ferret


   wnewman@netcom.com (Bill Newman) writes:

   >When I make two different instantiations of a generic package with the
   >same arguments, I understand the compiler treats them formally as two
   >different packages, which is OK with me.  However, I'd appreciate
   >knowing the compiler wouldn't actually output two redundant copies of
   >the corresponding (identical?) machine code, but instead share the
   >code.  I saw somewhere that the compiler is given considerable freedom
   >to share one instantiation between several arguments if it thinks it's
   >appropriate, which is also OK with me.  However, I haven't seen any
   >guarantee that the compiler won't output redundant copies for
   >instantiations with identical arguments.  Is there such a guarantee?

   Uh, repeat after me... Generic instantiation happens at run-time.

   Compilers can, and in some cases must generate code for generic
units.  But since in many cases, the number of copies of that generic
needed cannot be determined--even during execution--the compiler has
to be able to share generic code, even if some of the generic actual
parameters are different.

   This is the reason why this "fallacy" needs to be rooted out of
programmers thinking.  Generics in Ada are very powerful, but only if
you understand that the generic parameters are evaluated when the
generic instantiation is elaborated.  The same generic can be
elaborated thousands or millions of times during a single program
execution.

   Take for example, the generic packages in Text_IO.  It might seem
that since each of these packages has only a formal type parameter,
that every instance created from a single generic instance declaration
is the same.  Not so!  See the example program below.  The SUBtype
matching a generic formal type parameter can be one that is only
determined at run-time, in this case by reading in the bounds from the
terminal.  I put it in a loop so that you can see that each iteration
of the loop there is a new package Workday_IO created, and these
different packages accept different inputs.

-----------------------------------------------------------------------
with Text_IO;
with Calendar;
procedure Test_Generics is
  type Day is (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday);
  Start,Stop: Day;
  package Day_IO is new Text_IO.Enumeration_IO(Day);
begin
  loop
    Text_IO.New_Line;
    Text_IO.Put_Line(" Enter first day of work week: ");
    Day_IO.Get(Start);
    Text_IO.Put_Line(" Enter last day of work week: ");
    Day_IO.Get(Stop);
    if Start > Stop 
    then Text_IO.Put_Line(" Invalid range. Try again.");
    else
      declare
        subtype Workday is Day range Start..Stop;
        package Workday_IO is new Text_IO.Enumeration_IO(Workday);
        Worked: Workday;
      begin
        Text_IO.Put_Line(" Enter a day you work: ");
        Workday_IO.Get(Worked);
        Text_IO.Put_Line(" You worked " & Day'IMAGE(Worked) & '.');
      exception
        when Text_IO.Data_Error => 
          Text_IO.Put_Line(" Invalid day.  Try again.");
        when others =>
          Text_IO.Put_Line(" All done."); 
          return;
      end;
    end if;
  end loop;
end Test_Generics;
-----------------------------------------------------------------------------
spectre% a.ld test_generics -o TG
spectre% TG

 Enter first day of work week: 
Monday
 Enter last day of work week: 
Friday
 Enter a day you work: 
Tuesday
 You worked TUESDAY.

 Enter first day of work week: 
Wednesday
 Enter last day of work week: 
Saturday
 Enter a day you work: 
Tuesday
 Invalid day.  Try again.

 Enter first day of work week: 
Sunday
 Enter last day of work week: 
Wednesday
 Enter a day you work: 

\x03
spectre%
--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




  reply	other threads:[~1996-03-29  0:00 UTC|newest]

Thread overview: 80+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-03-27  0:00 some questions re. Ada/GNAT from a C++/GCC user Bill Newman
1996-03-27  0:00 ` Robert Dewar
1996-03-28  0:00   ` Norman H. Cohen
1996-03-28  0:00   ` Brian Rogoff
1996-03-29  0:00     ` John G. Volan
1996-03-30  0:00       ` Robert A Duff
1996-03-31  0:00         ` John G. Volan
1996-03-31  0:00           ` Mike Young
1996-04-02  0:00             ` Glenn H. Porter
1996-04-02  0:00               ` Robert Dewar
1996-04-02  0:00               ` Jonas Nygren
1996-04-03  0:00               ` Geert Bosch
1996-04-03  0:00                 ` Robert Dewar
1996-04-01  0:00           ` Bruce.Conroy
1996-04-01  0:00           ` Robert A Duff
1996-04-03  0:00             ` Scott Leschke
1996-04-04  0:00               ` AdaWorks
1996-03-31  0:00         ` Robert Dewar
1996-04-01  0:00           ` Norman H. Cohen
1996-03-30  0:00       ` Mike Young
1996-03-30  0:00         ` Ted Dennison
1996-03-31  0:00           ` Mike Young
1996-04-01  0:00       ` Norman H. Cohen
1996-04-01  0:00         ` Mike Young
1996-04-02  0:00           ` Robert Dewar
1996-04-02  0:00           ` Norman H. Cohen
1996-04-02  0:00           ` David Shochat
1996-04-02  0:00             ` Mike Young
1996-04-01  0:00         ` Robert A Duff
1996-04-01  0:00           ` Mike Young
1996-04-02  0:00             ` Robert A Duff
1996-04-02  0:00             ` Norman H. Cohen
1996-03-28  0:00 ` Ted Dennison
1996-03-29  0:00   ` Adam Beneschan
1996-03-28  0:00 ` Scott Leschke
1996-03-29  0:00   ` Robert I. Eachus [this message]
1996-03-29  0:00   ` Robert A Duff
1996-03-30  0:00     ` Richard Pitre
1996-03-30  0:00       ` Robert A Duff
1996-03-31  0:00         ` AdaWorks
1996-04-01  0:00           ` Robert A Duff
1996-04-01  0:00             ` AdaWorks
1996-04-01  0:00               ` Mike Young
1996-04-02  0:00                 ` AdaWorks
1996-04-02  0:00                 ` Robert Dewar
1996-04-01  0:00             ` Ken Garlington
1996-04-01  0:00               ` Robert A Duff
1996-04-02  0:00                 ` Ken Garlington
1996-04-02  0:00                   ` Robert A Duff
1996-04-02  0:00                     ` Ken Garlington
1996-04-02  0:00                       ` Robert A Duff
1996-04-03  0:00                         ` Ken Garlington
1996-04-09  0:00                           ` Matt Kennel
1996-04-03  0:00                         ` David Emery
1996-04-02  0:00                 ` Tucker Taft
1996-04-02  0:00                   ` Felaco
1996-04-02  0:00                     ` Robert Dewar
1996-04-03  0:00                     ` Mark A Biggar
1996-04-01  0:00             ` Norman H. Cohen
1996-04-01  0:00         ` Richard A. O'Keefe
1996-04-01  0:00           ` Robert A Duff
1996-04-01  0:00         ` Robert Dewar
1996-04-02  0:00       ` Robert I. Eachus
1996-03-29  0:00   ` Bill Newman
1996-03-29  0:00 ` Robert A Duff
1996-03-29  0:00   ` Brian Rogoff
1996-04-01  0:00     ` Mark A Biggar
1996-04-01  0:00       ` Robert A Duff
1996-03-30  0:00   ` Iterators (was Re: some questions re. Ada/GNAT from a C++/GCC user) Robert I. Eachus
1996-03-31  0:00     ` Mike Young
1996-03-31  0:00       ` Fergus Henderson
1996-04-01  0:00   ` Robert I. Eachus
     [not found]   ` <4jlj79$h1k@Nntp1.mcs.net>
1996-04-01  0:00     ` some questions re. Ada/GNAT from a C++/GCC user Robert A Duff
1996-04-02  0:00       ` Kevin Cline
1996-04-02  0:00         ` Robert A Duff
1996-04-04  0:00   ` Jon S Anthony
1996-03-30  0:00 ` Simon Wright
1996-04-01  0:00 ` Laurent Guerby
1996-04-01  0:00   ` Robert A Duff
  -- strict thread matches above, loose matches on Subject: below --
1996-03-28  0:00 Simon Johnston
replies disabled

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