comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey D. Cherry" <jdcherry@utech.net>
Subject: Re: Newbe Q: generic array not working OK
Date: 2000/03/13
Date: 2000-03-13T00:00:00+00:00	[thread overview]
Message-ID: <38CD1EB7.21EA523F@utech.net> (raw)
In-Reply-To: 38CBAE60.621F6A2D@skynet.be

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




  parent reply	other threads:[~2000-03-13  0:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-03-12  0:00 Newbe Q: generic array not working OK Erik Pessers
2000-03-13  0:00 ` swhalen
2000-03-13  0:00 ` Jeffrey D. Cherry [this message]
2000-03-14  0:00   ` Ehud Lamm
2000-03-15  0:00     ` Tucker Taft
replies disabled

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