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,ff52d623df1ca1d6 X-Google-Attributes: gid103376,public From: "Jeffrey D. Cherry" Subject: Re: Newbe Q: generic array not working OK Date: 2000/03/13 Message-ID: <38CD1EB7.21EA523F@utech.net>#1/1 X-Deja-AN: 596914402 Content-Transfer-Encoding: 7bit References: <38CBAE60.621F6A2D@skynet.be> X-Accept-Language: en,pdf Content-Type: text/plain; charset=us-ascii X-Trace: azure.impulse.net 952966915 192 207.154.66.216 Organization: Logicon MIME-Version: 1.0 Newsgroups: comp.lang.ada Date: 2000-03-13T00:00:00+00:00 List-Id: Erik, I know Mr. Barnes book rather well. The example comes from the chapter on generics and builds off an example from a previous chapter. The point of the example is to show how a general binary operator can be applied to all elements of an input array regardless of the data type of the elements in the array. Your code gets an error because the array type you're using as the actual argument in the instantiation is a constrained array type. The generic function requires an unconstrained array type. The unconstrained array type is used as the generic formal parameter so that any variable declared of type Vec (which should be an array with indexes of type Index, and elements of type Item), can be passed to the instantiation of function Apply. I believe the best way to show the difference is to provide the complete example from Mr. Barnes book. You can compare the code below to your original code. Please note that I've removed the unnecessary wrapper package T. Also note that due to Mr. Barnes' use of attributes, the input array to an instantiation of function Apply can span any index range within the discrete type provided in the instantiation. Examples of this are also shown in procedure test_apply. -- file apply.ads generic type Index is (<>); type Item is private; type Vec is array(Index range <>) of Item; with function "+"(x,y : Item) return Item; function Apply(a : vec) return Item; -- file apply.adb function Apply(a : vec) return Item is r : Item := a(a'first); begin -- Apply for i in Index'succ(a'first)..a'last loop r := r + a(i); end loop; return r; end Apply; -- file test_apply.adb with Apply; with Ada.Text_IO; with Ada.Float_Text_IO; procedure test_apply is type vec is array(integer range <>) of float; function Sum is new Apply(integer, float, vec, "+"); function Prod is new Apply(integer, float, vec, "*"); function Diff is new Apply(integer, float, vec, "-"); s : vec(11..15) := (others => 2.0); p : vec(1..5) := (others => 2.0); d : vec(-25..-21) := (others => 2.0); procedure Write_Results( Leader_Text : in string; Original_Array : in Vec; Result : in float) is procedure Put_Elements(a : in Vec) is begin -- Put_Elements Ada.Text_IO.Put("("); Ada.Float_Text_IO.Put(a(a'first), Fore => 0, Aft => 1, Exp => 0); for i in integer'succ(a'first) .. a'last loop Ada.Text_IO.Put(", "); Ada.Float_Text_IO.Put(a(i), Fore => 0, Aft => 1, Exp => 0); end loop; Ada.Text_IO.Put(")"); end Put_Elements; begin -- Write_Results Ada.Text_IO.Put(Leader_Text); Put_Elements(Original_Array); Ada.Text_IO.Put(" is "); Ada.Float_Text_IO.Put(Result, Fore => 0, Aft => 1, Exp => 0); Ada.Text_IO.New_Line; end Write_Results; begin -- test_apply Write_Results("Sum of ", s, Sum(s)); Write_Results("Product of ", p, Prod(p)); Write_Results("Difference of ", d, Diff(d)); end test_apply; -- Regards, Jeffrey D. Cherry Senior IV&V Analyst Logicon Space and Information Operations Logicon Inc. a Northrop Grumman company