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,FREEMAIL_FROM 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!news3.google.com!news.glorb.com!wn14feed!worldnet.att.net!bgtnsc04-news.ops.worldnet.att.net.POSTED!53ab2750!not-for-mail Newsgroups: comp.lang.ada Subject: Re: Discriminant computation problem From: Jim Rogers References: User-Agent: Xnews/5.04.25 Message-ID: Date: Mon, 15 Nov 2004 03:24:14 GMT NNTP-Posting-Host: 12.73.184.69 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc04-news.ops.worldnet.att.net 1100489054 12.73.184.69 (Mon, 15 Nov 2004 03:24:14 GMT) NNTP-Posting-Date: Mon, 15 Nov 2004 03:24:14 GMT Organization: AT&T Worldnet Xref: g2news1.google.com comp.lang.ada:6198 Date: 2004-11-15T03:24:14+00:00 List-Id: Sandro Magi wrote in 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; You might want to reconsider your design. First of all, your chosen example is a very poor way to represent a bit vector in Ada. This appears to be an Ada translation of a C or C++ programming approach. A more Ada-like way to accomplish your ends is: type Bit_Vector is array(Positive range <>) of Boolean; pragma Pack(Bit_Array); If you want to hide this implementation you can provide a package such as: package Bit_Vectors is type Bit_Vector(Max_Size : Positive) is private; .... private type Internal_Vector is array(Positive range <>) of Boolean; pragma Pack(Internal_Vector); type Bit_Vector(Max_Size : Positive) is record Buffer : Internal_Vector(Max_Size); end record; end Bit_Vectors; Each bit is individually addressable through array indexing. A packed array of boolean results in each boolean element occupying a single bit. There is no advantage in using an Integer type as an underlying element size. Integer is always a signed type. You will always error by not accounting for the sign bit. My second point deals with your desire to perform a calculation on a parameterized value. The more acceptable way to accomplish such an end is the use of a generic package rather than a record discriminant. The use of a generic package provides encapsulation and information hiding for your operations that prevent you from unnecessarily exposing internal data structures. Jim Rogers