comp.lang.ada
 help / color / mirror / Atom feed
* TYPE'Size in static expression
@ 2005-10-21  8:04 Francisco J. Montoya
  2005-10-21  8:55 ` Dmitry A. Kazakov
  0 siblings, 1 reply; 7+ messages in thread
From: Francisco J. Montoya @ 2005-10-21  8:04 UTC (permalink / raw)


Hello all.

I'm trying to write a generic (container) package whose only parameter 
is a type BaseType. I need to define an array of elements of this type 
(some kind of buffer) but I'd like the size of this buffer to be 
approximately constant regardless of the size of each BaseType element. 
My first attempt was to make some definitions like the following:

  Buffer_Size : constant := 1024; -- 1Kb buffer
  Num_Elements : constant := 8 * Buffer_Size / BaseType'Size; -- Wrong!
  type Buffer is array (1..Num_Elements) of BaseType;

But it's not possible to define Num_Elements this way, since 
BaseType'Size may not appear in a constant expression. I'm interested in 
storing only definite (constrained) types, so I may assume that 
BaseType'Size is a known positive value for any given BaseType. Also, I 
may assume that BaseType'Size <= 8 * Buffer_Size, so the buffer will 
always consist of at least one element.

So far I haven't succeeded in finding a suitable alternative to the 
former (illegal) expression. Of course, I always could add a new (value) 
parameter to the package, and force the instantiation to be like:

   package MyPac is new GenPac (BaseType, BaseType'Size);

but this is something I'd like to avoid as much as possible.

Comments and suggestions are welcome.

Regards,



-- 
Francisco J. Montoya
Depto. Informatica y Sistemas, Facultad de Informatica
(E-30071 Campus de Espinardo) Universidad de Murcia (Spain)
Voice: +34 968 364620, Fax: +34 968 364151,
e-mail: fmontoya AT dif.um.es




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: TYPE'Size in static expression
  2005-10-21  8:04 TYPE'Size in static expression Francisco J. Montoya
@ 2005-10-21  8:55 ` Dmitry A. Kazakov
  2005-10-21 22:09   ` Jeffrey R. Carter
  2005-10-24 10:56   ` Francisco J. Montoya
  0 siblings, 2 replies; 7+ messages in thread
From: Dmitry A. Kazakov @ 2005-10-21  8:55 UTC (permalink / raw)


On Fri, 21 Oct 2005 10:04:59 +0200, Francisco J. Montoya wrote:

> I'm trying to write a generic (container) package whose only parameter 
> is a type BaseType. I need to define an array of elements of this type 
> (some kind of buffer) but I'd like the size of this buffer to be 
> approximately constant regardless of the size of each BaseType element. 
> My first attempt was to make some definitions like the following:
> 
>   Buffer_Size : constant := 1024; -- 1Kb buffer
>   Num_Elements : constant := 8 * Buffer_Size / BaseType'Size; -- Wrong!

Num_Elements : constant Integer :=
   8 * Buffer_Size / BaseType'Size;

>   type Buffer is array (1..Num_Elements) of BaseType;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: TYPE'Size in static expression
  2005-10-21  8:55 ` Dmitry A. Kazakov
@ 2005-10-21 22:09   ` Jeffrey R. Carter
  2005-10-24 10:56   ` Francisco J. Montoya
  1 sibling, 0 replies; 7+ messages in thread
From: Jeffrey R. Carter @ 2005-10-21 22:09 UTC (permalink / raw)


Dmitry A. Kazakov wrote:

> Num_Elements : constant Integer :=
>    8 * Buffer_Size / BaseType'Size;

Right. At least if you allow types with a negative size. Otherwise, "constant 
Natural" might be more descriptive.

-- 
Jeff Carter
"Nobody expects the Spanish Inquisition!"
Monty Python's Flying Circus
22



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: TYPE'Size in static expression
  2005-10-21  8:55 ` Dmitry A. Kazakov
  2005-10-21 22:09   ` Jeffrey R. Carter
@ 2005-10-24 10:56   ` Francisco J. Montoya
  2005-10-24 11:45     ` Dmitry A. Kazakov
  2005-10-24 19:15     ` Jeffrey R. Carter
  1 sibling, 2 replies; 7+ messages in thread
From: Francisco J. Montoya @ 2005-10-24 10:56 UTC (permalink / raw)


Thank you very much for your quick answer, Dmitry.

That, indeed, solved my problem, but I still do not finish in 
understanding why what I wrote did not work. The expression I wrote is 
supposed to be of "Universal Integer Type", isn't it? So adding the 
explicit type "Integer" to the constant definition should not make much 
difference.

Anyhow, I'm just a newbie and a clumsy Ada programmer, so don't pay too 
much attention to what I say ;)

Regards, and thanks again.


Dmitry A. Kazakov escribió:
> On Fri, 21 Oct 2005 10:04:59 +0200, Francisco J. Montoya wrote:
> 
> 
>>My first attempt was to make some definitions like the following:
>>
>>  Buffer_Size : constant := 1024; -- 1Kb buffer
>>  Num_Elements : constant := 8 * Buffer_Size / BaseType'Size; -- Wrong!
> 
> 
> Num_Elements : constant Integer :=
>    8 * Buffer_Size / BaseType'Size;
> 
> 
>>  type Buffer is array (1..Num_Elements) of BaseType;
> 
> 


-- 
Francisco J. Montoya
Depto. Informatica y Sistemas, Facultad de Informatica
(E-30071 Campus de Espinardo) Universidad de Murcia (Spain)
Voice: +34 968 364620, Fax: +34 968 364151,
e-mail: fmontoya AT dif.um.es




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: TYPE'Size in static expression
  2005-10-24 10:56   ` Francisco J. Montoya
@ 2005-10-24 11:45     ` Dmitry A. Kazakov
  2005-10-24 19:15     ` Jeffrey R. Carter
  1 sibling, 0 replies; 7+ messages in thread
From: Dmitry A. Kazakov @ 2005-10-24 11:45 UTC (permalink / raw)


On Mon, 24 Oct 2005 12:56:31 +0200, Francisco J. Montoya wrote:

> That, indeed, solved my problem, but I still do not finish in 
> understanding why what I wrote did not work. The expression I wrote is 
> supposed to be of "Universal Integer Type", isn't it? So adding the 
> explicit type "Integer" to the constant definition should not make much 
> difference.

"Universal integer constant" have to be static. But just constant T need
not to be. This makes the difference. So you could also write:

type Buffer is array (1..8 * Buffer_Size / BaseType'Size) of BaseType;

Ada's rules about static expressions are a bit tricky. But for a generic
body, it is clear that BaseType'Size cannot be static.

[ Or, well, it could be, if generics were treated like C++ templates. But
then sharing of generic bodies would be difficult, if possible. ]

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: TYPE'Size in static expression
  2005-10-24 10:56   ` Francisco J. Montoya
  2005-10-24 11:45     ` Dmitry A. Kazakov
@ 2005-10-24 19:15     ` Jeffrey R. Carter
  2005-10-24 20:15       ` Robert A Duff
  1 sibling, 1 reply; 7+ messages in thread
From: Jeffrey R. Carter @ 2005-10-24 19:15 UTC (permalink / raw)


Francisco J. Montoya wrote:

> That, indeed, solved my problem, but I still do not finish in 
> understanding why what I wrote did not work. The expression I wrote is 
> supposed to be of "Universal Integer Type", isn't it? So adding the 
> explicit type "Integer" to the constant definition should not make much 
> difference.

Named numbers must be static. 'Size is only static for certain types. 'Size is 
never static for generic formal types.

Typed constants, on the other hand, need not be static. That's why "constant 
Integer :=" works here, but "constant :=" doesn't.

-- 
Jeff Carter
"Ditto, you provincial putz?"
Blazing Saddles
86



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: TYPE'Size in static expression
  2005-10-24 19:15     ` Jeffrey R. Carter
@ 2005-10-24 20:15       ` Robert A Duff
  0 siblings, 0 replies; 7+ messages in thread
From: Robert A Duff @ 2005-10-24 20:15 UTC (permalink / raw)


"Jeffrey R. Carter" <spam@spam.com> writes:

> Francisco J. Montoya wrote:
> 
> > That, indeed, solved my problem, but I still do not finish in
> > understanding why what I wrote did not work. The expression I wrote is
> > supposed to be of "Universal Integer Type", isn't it? So adding the
> > explicit type "Integer" to the constant definition should not make
> > much difference.
> 
> Named numbers must be static. 'Size is only static for certain
> types. 'Size is never static for generic formal types.
> 
> Typed constants, on the other hand, need not be static. That's why
> "constant Integer :=" works here, but "constant :=" doesn't.

Right.

A few more hints about Ada terminology: the former (X: constant T :=
<expr>) is called a constant.  The expression may be a run-time
quantity.  The latter (X: constant := <expr>) is called a named number.
The expression must be static, which means its value must be known at
compile time, based on certain rules, which involve the structure of the
expression and the declarations it refers to (e.g. "2+2" is static.
"Mumble+2" is static if Mumble is a static constant.  Etc)

Named numbers are a kludge.  I tend to stay away from them, except when
they're necessary.

Some languages use the word "constant" to refer to what Ada means by
"static".  Some languages say "immutable" to mean roughly what Ada means
by "constant".  It's all very confusing.

As Jeff pointed out above, T'Size is not static if T is a generic formal
type.  That's because, when compiling the generic, the compiler can't
know the value of T'Size.  Nonetheless, most Ada compilers will treat
T'Size as a compile-time-known value in most cases -- most compilers
generate separate code for each instance of the generic.

- Bob



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2005-10-24 20:15 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-10-21  8:04 TYPE'Size in static expression Francisco J. Montoya
2005-10-21  8:55 ` Dmitry A. Kazakov
2005-10-21 22:09   ` Jeffrey R. Carter
2005-10-24 10:56   ` Francisco J. Montoya
2005-10-24 11:45     ` Dmitry A. Kazakov
2005-10-24 19:15     ` Jeffrey R. Carter
2005-10-24 20:15       ` Robert A Duff

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