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 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,3abdaa39e1c27f49 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!fu-berlin.de!uni-berlin.de!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Discriminant computation problem Date: Mon, 15 Nov 2004 17:02:11 +0100 Message-ID: <1csd5r45jm41a$.119v7kqdyy147.dlg@40tude.net> References: <7e2ad2d.0411150602.79ee1251@posting.google.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de jVZ5mZ8p02JKhBYESAxvFgAB++CyQ3y+tmoSrv3KdS6/0fqn0= User-Agent: 40tude_Dialog/2.0.12.1 Xref: g2news1.google.com comp.lang.ada:6208 Date: 2004-11-15T17:02:11+01:00 List-Id: 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