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 10.236.197.39 with SMTP id s27mr1654923yhn.36.1397594253774; Tue, 15 Apr 2014 13:37:33 -0700 (PDT) X-Received: by 10.50.97.34 with SMTP id dx2mr596028igb.0.1397594253580; Tue, 15 Apr 2014 13:37:33 -0700 (PDT) Path: border1.nntp.dca.giganews.com!nntp.giganews.com!cm18no3262697qab.0!news-out.google.com!en3ni102igc.0!nntp.google.com!l13no7345990iga.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Tue, 15 Apr 2014 13:37:32 -0700 (PDT) In-Reply-To: <3c4668e7-73f6-4a99-a96d-619639ea49e8@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=66.126.103.122; posting-account=KSa2aQoAAACOxnC0usBJYX8NE3x3a1Xq NNTP-Posting-Host: 66.126.103.122 References: <1ffb84f0-5e50-4807-90ff-dfdfac11c501@googlegroups.com> <596f076f-919e-4cd2-bb2e-ad0134e0d5c3@googlegroups.com> <3c4668e7-73f6-4a99-a96d-619639ea49e8@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <93fec78f-99a6-4052-921b-6f9d7b13b869@googlegroups.com> Subject: Re: Problem with generic package From: Adam Beneschan Injection-Date: Tue, 15 Apr 2014 20:37:33 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Xref: number.nntp.dca.giganews.com comp.lang.ada:185750 Date: 2014-04-15T13:37:32-07:00 List-Id: On Tuesday, April 15, 2014 1:12:33 PM UTC-7, Laurent wrote: > Hi >=20 >=20 >=20 > Hm yes and no. I am trying to get rid of Swap_Generic and Sort_Generic. T= rying to combine those together with my generic search function in one gene= ric package: array_generics (stupid name have to find something else). Have= just posted them for completeness but forgotten to write it (if its clear = for me doesn't mean it is for someone else). >=20 > Sorry for the confusion. >=20 > If I leave the swap_generic.ads and with'ing it, comment the swap in the = array_generic.adb out then the instantiation works without changing anythin= g else. >=20 > If I remove the swap_generic.adb from the library folder, remove the with= 'ing, reload project... >=20 > and try to instantiate the swap_generic from the package I get this error= : >=20 > expect generic subprogram in instantiation. >=20 > No idea why the compiler complains. Should be the same thing as before.= =20 No--it definitely is not the same. In the version with top-level Sort_Gene= ric and Swap_Generic, both of those are generics, and you would instantiate= them. But if you have a generic package Array_Generics with two *non-gene= ric* procedures Sort_Generic and Swap_Generic, you would only instantiate A= rray_Generics--never Sort_Generic or Swap_Generic. You can only instantiat= e generics, and generics are things declared with the word "generic". =20 In this case, if you were *outside* of Array_Generics, you would instantiat= e Array_Generics: package My_Record_Arrays is new Array_Generics (Element_Type =3D> My_Record, Index_Type =3D> [something],=20 List_Type =3D> [something], Compare =3D> [something]); and then you could use Swap_Generic like this: My_Record_Arrays.Swap_Generic (v1, v2); (Of course, it wouldn't be a generic any more, so the name is not appropria= te. Also, if you don't want programs to have to provide Index_Type, List_T= ype, and Compare, just to use Swap, then you need to move Swap *out* of Arr= ay_Generics. It's not an array routine, so maybe it doesn't belong there a= nyway.) In the example you posted, you're already inside Array_Generics, so you can= 't have Array_Generics instantiate itself. In this case, you don't want to= instantiate Swap_Generic, you just call it. It looks like you don't quite understand how generics work. When you insta= ntiate Array_Generics, like this: package My_Record_Arrays is new Array_Generics (Element_Type =3D> My_Record, Index_Type =3D> Natural,=20 List_Type =3D> My_Record_Array, Compare =3D> My_Compare_Function); =20 it's almost as if you had the specification and body of Array_Generics righ= t there in your code, with the name Array_Generics replaced by My_Record_Ar= rays, Element_Type replaced by My_Record, Index_Type replace by Natural, an= d so on. (The effect isn't exactly the same as replacing names; there are = some subtleties that make it different. But for the purpose of understandi= ng how they work, it's close enough.) So now you have an instantiated pack= age spec that looks something like this: package My_Record_Arrays is procedure Swap_Generic (Value_1, Value_2 : in out MY_RECORD_TYPE);= =20 procedure Sort_Generic (List : in out MY_RECORD_ARRAY); ... and an instantiated package body that looks something like: package body My_Record_Arrays is procedure Swap_Generic (Value_1, Value_2 : in out MY_RECORD_TYPE) i= s=20 ... end Swap_Generic; procedure Sort_Generic (List : in out MY_RECORD_ARRAY) is=20 ... end Sort_Generic; And within this (pretend) instantiated package body, you wouldn't need to i= nstantiate Swap_Generic, would you? You'd just call it, because all the ge= neric parameters have been replaced already. And in the rest of your progr= am, if you had an package spec that looked like the above, you'd use the sw= ap procedure by calling My_Record_Arrays.Swap_Generic(X,Y), right? Well, t= hat's how you'd call it when you do it with a generic. Anyway, that's roughly how you need to think about generics. Hope this hel= ps clear things up. -- Adam