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: "Nick Roberts" Newsgroups: comp.lang.ada Subject: Re: Discriminant computation problem Date: Mon, 15 Nov 2004 22:11:16 -0000 Message-ID: <2vsns5F2p6vbpU1@uni-berlin.de> References: X-Trace: news.uni-berlin.de 2htZ3AoI66Ypw5wkjn1Hzgg0EPShSTQSIJfZUA87geaj51ev4= X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-RFC2646: Format=Flowed; Original Xref: g2news1.google.com comp.lang.ada:6211 Date: 2004-11-15T22:11:16+00:00 List-Id: "Sandro Magi" wrote in message news:NmTld.122$rc.97840@news20.bellglobal.com... > Just learning Ada and I came across this little issue. I can't perform a > computation on a discriminant. Suppose I want to create a bit vector type > that uses an array of Integers as its underlying storage type (just an > example). I want the user to provide the length of the bit vector in bits, > but I need to define the upper bound on the Integer array in Integer'Size > units. > > type Int_Array is array (Positive range <>) of Integer; > > type Bit_Vector (Max_Bit_Size : Positive) is > record > Series : Int_Array (Positive'First .. Max_Bit_Size/Integer'Size); > end record; > > The compiler error gnat produces: "discriminant in constraint must appear > alone". Is there something I'm doing wrong, or is the above simply not > possible? You are not doing anything wrong, as such. It's unfortunately necessary for Ada to restrict the usage of discriminants in component constraints, so that all compilers can be assured of being able to compute the size of a record from its discriminants at any point without having to perform a computation of (potentially) unlimited complexity or which might have side effects. > I can solve this either by breaking the encapsulation of my type so that > the > user must perform the computation ahead of time (not preferable), or by > creating a special function which computes the real upper bound. So the > user would have to instantiate: > > bv : Bit_Vector(Real_Upper_Bound(1024)); --1024 bits > > Instead of: > > bv : Bit_Vector(1024); --1024 bits > > Does anyone have a better answer? I don't have a better answer. I can only additionally suggest that you could declare an access type, and then an allocator function, as well as interrogation functions. For example: Bits_Per_Element: constant := Integer'Size; -- say Max_Element_Index: constant := 1023; -- say Max_Bit_Number: constant := Max_Element_Index*Bits_Per_Element-1; subtype Bit_Number is Integer range 0..Max_Bit_Number; type Bit_Vector_Element is mod 2**Bits_Per_Element; subtype Element_Index is Integer range 0..Max_Element_Index; type Element_Array is array (Element_Index range <>) of Bit_Vector_Element; type Bit_Vector (Max: Element_Index) is record Max_Bit: Bit_Number; Elements: Element_Array(0..Max); end record; type Dynamic_Bit_Vector is access Bit_Vector; function New_Bit_Vector (Max_Bit: in Bit_Number) return Dynamic_Bit_Vector; ... function New_Bit_Vector (Max_Bit: in Bit_Number) return Dynamic_Bit_Vector is Max: constant Element_Index := Max_Bit/Bits_Per_Element; begin return new Bit_Vector'( Max => Max, Max_Bit => Max_Bit, Elements => (0..Max => 0) ); end New_Bit_Vector; I've found this problem to be a pain myself, in the past. You just just have to 'program around it'. -- Nick Roberts