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.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 2002:a37:6311:: with SMTP id x17mr805844qkb.93.1586465151355; Thu, 09 Apr 2020 13:45:51 -0700 (PDT) X-Received: by 2002:a9d:4c8c:: with SMTP id m12mr1563851otf.5.1586465150753; Thu, 09 Apr 2020 13:45:50 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!news.gegeweb.eu!gegeweb.org!usenet-fr.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Thu, 9 Apr 2020 13:45:50 -0700 (PDT) In-Reply-To: <5e8edf69$0$18432$e4fe514c@news.kpn.nl> Complaints-To: groups-abuse@google.com Injection-Info: google-groups.googlegroups.com; posting-host=70.109.61.2; posting-account=QF6XPQoAAABce2NyPxxDAaKdAkN6RgAf NNTP-Posting-Host: 70.109.61.2 References: <5e8edf69$0$18432$e4fe514c@news.kpn.nl> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <15129999-1197-4f3c-919b-839fd17e7ee5@googlegroups.com> Subject: Re: Using Generic Pasckages From: Jere Injection-Date: Thu, 09 Apr 2020 20:45:51 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Xref: reader01.eternal-september.org comp.lang.ada:58339 Date: 2020-04-09T13:45:50-07:00 List-Id: On Thursday, April 9, 2020 at 4:40:11 AM UTC-4, ldries46 wrote: > I have created some generic packages. tese are packages that only do the= =20 > same thing but with other types. Till now I did only need these packages= =20 > without interaction with between packages using the same type. I just=20 > can declare them: > Package AA is new BB(type); then calling them as > A :=3D AA.Get_Value; > No other declaration seemed to be neccesary . >=20 > Now I have the following situation: > Package AA is new BB(integer); > Package CC is new BB(integer); > and a case where I have several statements using Package AA in one=20 > condition and CC the other case. > I want=C2=A0 to do that by using: > if D then EE :=3D AA; else EE :=3D CC; end if; > But I cannot find in the documentation=C2=A0 how the declaration of EE sh= ould=20 > be made. >=20 > Of course there is the possibility to create the same progrram without=20 > using EE but that is far less readable and thus creating more possible=20 > errors. >=20 > If I have to do this without the generic packages I already have the=20 > program alse will be less readable. You've already had suggestions for polymorphism or using if/else=20 scaffolding. For run time decisions like this, if both packages are using the same types, I would recommend polymorphism. However if you prefer to stick with generics, you make the scaffolding a bit less painful by using another generic. Assuming your generic BB has a specification like this: generic type Some_Type is private; package BB is procedure Set_Value(V : Some_Type); function Get_Value return Some_Type; end BB; You can abstract out all your logic using it with another generic similar to: generic with package EE is new BB(Some_Type =3D> Integer); procedure Do_Stuff; =20 procedure Do_Stuff is =20 V : Integer :=3D 23; =20 begin Put_Line(Integer'Image(EE.Get_Value)); EE.Set_Value(V); Put_Line(Integer'Image(EE.Get_Value)); =20 -- All your other BB logic you want to do end Do_Stuff; then you pair it with the if/else or case structure=20 and locally declared blocks: Put_Line("Hello, world!"); AA.Set_Value(10); CC.Set_Value(20); =20 if D then=20 declare procedure P is new Do_Stuff(AA); begin P; end; else declare procedure P is new Do_Stuff(CC); begin P; end; end if; It still has some scaffolding, but it is more=20 readable then tons of if/else blocks scattered throughout the code. If your packages don't=20 have to rely on Integer, you can change the=20 specification to: generic with package EE is new BB(<>); procedure Do_Stuff; But if your logic actually does rely on knowing it is an integer type, you will get compiler errors. Full compilable example below: with Ada.Text_IO; use Ada.Text_IO; procedure Hello is =20 generic type Some_Type is private; package BB is procedure Set_Value(V : Some_Type); function Get_Value return Some_Type; end BB; =20 package body BB is Value : Some_Type; =20 procedure Set_Value(V : Some_Type) is begin Value :=3D V; end Set_Value; =20 function Get_Value return Some_Type is (Value); end BB; =20 package AA is new BB(Integer); package CC is new BB(Integer); =20 generic with package EE is new BB(<>); procedure Do_Stuff_1; =20 procedure Do_Stuff_1 is =20 V : EE.Some_Type :=3D EE.Get_Value; =20 begin EE.Set_Value(V); end Do_Stuff_1; =20 generic with package EE is new BB(Some_Type =3D> Integer); procedure Do_Stuff_2; =20 procedure Do_Stuff_2 is =20 V : Integer :=3D 23; =20 begin Put_Line(Integer'Image(EE.Get_Value)); EE.Set_Value(V); Put_Line(Integer'Image(EE.Get_Value)); end Do_Stuff_2; =20 D : Boolean :=3D True; begin Put_Line("Hello, world!"); AA.Set_Value(10); CC.Set_Value(20); =20 if D then=20 declare procedure P is new Do_Stuff_2(AA); begin P; end; else declare procedure P is new Do_Stuff_2(CC); begin P; end; end if; =20 end Hello;