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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,7bf4308ff88deca8 X-Google-Attributes: gid103376,public From: mheaney@ni.net (Matthew Heaney) Subject: Re: How I declare a 'mod' type within a record? Date: 1997/07/30 Message-ID: #1/1 X-Deja-AN: 260855359 References: <33DF3FD2.20D2@mail.connect.usq.edu.au> Organization: Estormza Software Newsgroups: comp.lang.ada Date: 1997-07-30T00:00:00+00:00 List-Id: In article <33DF3FD2.20D2@mail.connect.usq.edu.au>, q9522772@mail.connect.usq.edu.au wrote: >Also, I can see how this could be made into a generic package but what I >need is multiple time-series of different sizes. > >with Text_IO; use Text_IO; >procedure Aging is > > type Number is new Float; > type Buffer is array(Integer range <>) of Number; > type Time_Series(Size : Integer) is > record > History : Buffer(0..Size-1); > Head : mod Size; > end record; > > X : Time_Series(3); >begin > ... >end Aging; >From one Matthew to another: No, you can't do this. The modulus for the type must be a static expression. The reason for this is so that the compiler knows how much space to allocate for the type at compile time; this admits a more efficient implementation. All scalar types must have constraints that are determinable at compile time, that's why there's a special syntax for first-named subtypes (they are marked with the word "type" instead of "subtype"). Note that you can't use a discriminant for the range constraint of a component that's a numeric type, for example type RT (Max : Positive) is record C : Integer range 0 .. Max; -- range constraint illegal end record; is illegal. Furthermore, even though you can use the discriminant as an index constraint, you can't use the discriminant value in an expression for an index constraint, ie type RT (Size : Positive) is record B : Buffer (0 .. Size - 1); -- index expression is illegal end record; This is also illegal. However, you can use the discriminant for an index constraint of an array component: type RT (Max : Positive) is record C : String (1 .. Max); end record; is perfectly legal. There is a time when you can use the discriminant as part of an expression, and that's when the expression is used for the initial value of a component: type RT (Default : Integer) is record C : Integer := 10 * Default + 3; -- expression OK end record; You can even pass the discrimint to a function that returns the value used for an initial value of a component (you may have a more complex initial value to calculate). Note that staticness only applies to scalar types, because the compiler picks the hardware types for objects at compile time. But records and arrays are very dynamic: procedure P (N : Positive) is type Array_Type is array (Positive range 1 .. N) of Integer range 1 .. N; type Record_Type is record C : Array_Type; I : Integer; C2 : Array_Type; end record; begin At compile time, you don't know how big Array_type or Record_Type will be, so there's much more freedom there compared to non-composite types. You can't use generic formal parameters as the constraints in a type declaration either, because formal paramters aren't static. So you can't do this generic I : Integer; package GP is type FT is digits I; type MT is mod I; end; This is illegal, because the digit constraint and modulus (is that the correct term?) constraint must be static, and I is not a static expression. But you can use it for subtype constraints, in the normal way: generic I : Integer; package GP is subtype IT is Integer range 0 .. I; type MT is mod 10; subtype MST is MT range 0 .. I; type AType is array (Positive range 1 .. I) of IT; end; All legal (I think - I haven't tried to compile, but you get the idea), because subtype constraints don't have to be static. Hope that helps, Matt -------------------------------------------------------------------- Matthew Heaney Software Development Consultant (818) 985-1271