From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Discriminant computation problem
Date: Mon, 15 Nov 2004 17:02:11 +0100
Date: 2004-11-15T17:02:11+01:00 [thread overview]
Message-ID: <1csd5r45jm41a$.119v7kqdyy147.dlg@40tude.net> (raw)
In-Reply-To: 7e2ad2d.0411150602.79ee1251@posting.google.com
On 15 Nov 2004 06:02:39 -0800, Sandro Magi wrote:
> I was trying to avoid the extra step of instantiating a generic
> package. Furthermore, the types from one package would not be
> interoperable with the types from another package would they? ie.
> using my Bit_Vector example and assuming the package defines an Append
> operation:
>
> procedure Test is
> package Bit_Vector_1024 is new Bit_Vector(1024);
> package Bit_Vector_2048 is new Bit_Vector(2048);
>
> b1024 : Bit_Vector_1024.Bit_Vector;
> b2048 : Bit_Vector_2048.Bit_Vector;
> begin
> ...
> (initialize some data in each vector)
> ...
> --this would fail to type check wouldn't it?
> Append(Data=>b1024, To=>b2048);
> end Test;
>
> Sorry for asking something so obvious, but I can't check this myself
> at the moment.
Bit_Vector_1024.Bit_Vector and Bit_Vector_2048.Bit_Vector are two different
types, when they are declared in Bit_Vector as types. To avoid this you
should have a common ancestor for both. There could be three variants:
1. Constrained subtypes:
package Bit_Vectors is
type Vector is array (Integer range <>) of Boolean;
-- or
type Vector (Size : Natural) is private;
procedure Append (Data : Vector; To : in out Vector);
-- Note, Append is defined here. The common "ancestor"
-- is unconstrained Vector
...
end Bit_Vectors_Base;
generic
Size : Positive;
package Bit_Vectors.Fixed_Size is
subtype Bit_Vector is Vector (1..Size);
...
end Bit_Vectors.Fixed_Size;
procedure Test is
package Bit_Vector_1024 is new Bit_Vectors.Fixed_Size (1024);
package Bit_Vector_2048 is new Bit_Vectors.Fixed_Size (2048);
b1024 : Bit_Vector_1024.Bit_Vector;
b2048 : Bit_Vector_2048.Bit_Vector;
begin
Append (Data => b1024, To => b2048); -- This is OK
2. Tagged types derived from the same base:
package Bit_Vectors is
type Vector is abstract tagged ...
procedure Append (Data : Vector'Class; To : in out Vector) is
abstract;
-- Note, at least one of the parameters should be
-- class-wide. There is no full multiple dispatch in Ada!
-- The common ancestor there is abstract Vector.
...
end Bit_Vectors_Base;
generic
Size : Positive;
package Bit_Vectors.Fixed_Size is
type Bit_Vector is new Vector with record
-- Here the body uses Size
end;
procedure Append (Data : Vector'Class; To : in out Bit_Vector);
-- You have to implement it here for the case when To
-- is Bit_Vector, where Data is anything possible.
end Bit_Vectors.Fixed_Size;
procedure Test is
package Bit_Vector_1024 is new Bit_Vectors.Fixed_Size (1024);
package Bit_Vector_2048 is new Bit_Vectors.Fixed_Size (2048);
b1024 : Bit_Vector_1024.Bit_Vector;
b2048 : Bit_Vector_2048.Bit_Vector;
begin
Append (Data => b1024, To => b2048); -- This is OK
3. Do not use generics! Ada's unconstrained types are powerful and
efficient.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
next prev parent reply other threads:[~2004-11-15 16:02 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-11-15 1:11 Discriminant computation problem Sandro Magi
2004-11-15 3:24 ` Jim Rogers
2004-11-15 14:02 ` Sandro Magi
2004-11-15 15:16 ` Martin Krischik
2004-11-15 16:02 ` Dmitry A. Kazakov [this message]
2004-11-15 22:11 ` Nick Roberts
2004-11-15 23:25 ` tmoran
2004-11-16 20:00 ` Nick Roberts
2004-11-16 20:14 ` tmoran
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox