comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: How I declare a 'mod' type within a record?
Date: 1997/07/30
Date: 1997-07-30T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023680003007972312110001@news.ni.net> (raw)
In-Reply-To: 33DF3FD2.20D2@mail.connect.usq.edu.au


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
<mailto:matthew_heaney@acm.org>
(818) 985-1271




  reply	other threads:[~1997-07-30  0:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-07-30  0:00 How I declare a 'mod' type within a record? Matthew Kennedy
1997-07-30  0:00 ` Matthew Heaney [this message]
1997-07-31  0:00 ` Robert Dewar
1997-08-05  0:00 ` Mars Gralia
1997-08-05  0:00   ` John M. Mills
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox