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=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,f92fbb4a0420dd57 X-Google-Attributes: gid103376,public From: eachus@spectre.mitre.org (Robert I. Eachus) Subject: Re: some questions re. Ada/GNAT from a C++/GCC user Date: 1996/03/29 Message-ID: #1/1 X-Deja-AN: 144862967 references: organization: The Mitre Corp., Bedford, MA. newsgroups: comp.lang.ada Date: 1996-03-29T00:00:00+00:00 List-Id: 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:  spectre% -- Robert I. Eachus with Standard_Disclaimer; use Standard_Disclaimer; function Message (Text: in Clever_Ideas) return Better_Ideas is...