comp.lang.ada
 help / color / mirror / Atom feed
* Newbe Q: generic array not working OK
@ 2000-03-12  0:00 Erik Pessers
  2000-03-13  0:00 ` swhalen
  2000-03-13  0:00 ` Jeffrey D. Cherry
  0 siblings, 2 replies; 5+ messages in thread
From: Erik Pessers @ 2000-03-12  0:00 UTC (permalink / raw)


Hi,

Trying to better understand the "generic" concept, I did stumble over
a problem I do not fully understand (most propably because I'm new
to using Ada, coming from a Pascal/Modula-2 background).

I get errors when compiling the attached piece of code on
a Linux (Debian 2.2.10) PC using GNAT 3.11:

   gnatgcc -c test.adb
   test.adb:4:48: expect unconstrained array in instantiation of "Vec"
   test.adb:4:48: instantiation abandoned
   gnatmake: "test.adb" compilation error

Code is in fact taken from John Barnes book./

Please tell me what point I'm missing?

TIA.

t.ads :

  package t is
  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 ;
  end t ;


t.adb :

  package body t is
    function Apply (a : vec) return Item is
     r : Item := A(A'First) ;
    begin
      return (r) ;
    end Apply ;
   end t ;

test.adb:

with t ;
  procedure test is
    type Vec is array (Integer range 1..6) of Float ;
    function Sum is new t.Apply (Integer, Float, Vec, "+") ;
  begin
  null ;
  end test ;








--
Erik Pessers  <Erik.Pessers@ACM.ORG>






^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Newbe Q: generic array not working OK
  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
  1 sibling, 0 replies; 5+ messages in thread
From: swhalen @ 2000-03-13  0:00 UTC (permalink / raw)


I'm not sure I understand the question, but this works on
GNAT 3.12p (I got the same error as you on your version).

--  test.adb:
with Ada.Text_IO; use Ada.Text_IO;
with t ;
procedure test is
   --  type Vec is array (Integer range 1..6) of Float ;
   type Vec is array (Integer range <>) of Float ;
   function Sum is new t.Apply (Integer, Float, Vec, "+") ;
   subtype Vec6 is Vec (1..6);
   V6 : Vec6 := (6.6, 5.5, 4.4, 3.3, 2.2, 1.1);
begin
   Put_Line (Sum (V6)'Img);
end test ;


-- 
{===--------------------------------------------------------------===}
                Steve Whalen     swhalen@netcom.com
{===--------------------------------------------------------------===}




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Newbe Q: generic array not working OK
  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
  2000-03-14  0:00   ` Ehud Lamm
  1 sibling, 1 reply; 5+ messages in thread
From: Jeffrey D. Cherry @ 2000-03-13  0:00 UTC (permalink / raw)


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




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Newbe Q: generic array not working OK
  2000-03-13  0:00 ` Jeffrey D. Cherry
@ 2000-03-14  0:00   ` Ehud Lamm
  2000-03-15  0:00     ` Tucker Taft
  0 siblings, 1 reply; 5+ messages in thread
From: Ehud Lamm @ 2000-03-14  0:00 UTC (permalink / raw)


Maybe it is worth mentioning that the distinction between unconstrained
and constrained generic array paramters was introduced in Ada95. 
The interested reader can check the Rational, for the rational of this
decision (tightening the contract model).

Ehud Lamm mslamm@mscc.huji.ac.il
http://purl.oclc.org/NET/ehudlamm <== My home on the web 
Check it out and subscribe to the E-List- for interesting essays and more!






^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Newbe Q: generic array not working OK
  2000-03-14  0:00   ` Ehud Lamm
@ 2000-03-15  0:00     ` Tucker Taft
  0 siblings, 0 replies; 5+ messages in thread
From: Tucker Taft @ 2000-03-15  0:00 UTC (permalink / raw)


Ehud Lamm wrote:
> 
> Maybe it is worth mentioning that the distinction between unconstrained
> and constrained generic array paramters was introduced in Ada95.
> The interested reader can check the Rational, for the rational of this
> decision (tightening the contract model).

Not quite.  Ada 83 and Ada 95 both distinguish constrained generic
array parameters from unconstrained ones.  Ada 95 introduced
the distinction between "definite" private and "indefinite" private (aka "<>")
formal types.  Also, in Ada 95, array type matching is done statically
(at compile-time) rather than involving any run-time checks.

Here is a quote from Ada 83, 12.3.4(2): "... the formal type and the
actual type must be either both constrained or both unconstrained."

> 
> Ehud Lamm mslamm@mscc.huji.ac.il
> http://purl.oclc.org/NET/ehudlamm <== My home on the web
> Check it out and subscribe to the E-List- for interesting essays and more!

-- 
-Tucker Taft   stt@averstar.com   http://www.averstar.com/~stt/
Technical Director, Distributed IT Solutions  (www.averstar.com/tools)
AverStar (formerly Intermetrics, Inc.)   Burlington, MA  USA




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2000-03-15  0:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2000-03-14  0:00   ` Ehud Lamm
2000-03-15  0:00     ` Tucker Taft

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