comp.lang.ada
 help / color / mirror / Atom feed
From: bobduff@world.std.com (Robert A Duff)
Subject: Re: Static expressions and using 'Size
Date: 1996/03/22
Date: 1996-03-22T00:00:00+00:00	[thread overview]
Message-ID: <DooH3J.5rM@world.std.com> (raw)
In-Reply-To: 4isu7e$4d0@fozzie.sun3.iaf.nl

In article <4isu7e$4d0@fozzie.sun3.iaf.nl>,
Geert Bosch <geert@fozzie.sun3.iaf.nl> wrote:
>Because of portability, readability and safety I'd like not to calculate
>all record-sizes etc. myself, but have the compiler do that for me. The
>problem is however that expressions involving the 'Size attribute are
>not static and for that reason cannot be used in constants.

You are confusing "named numbers" and "constants".  The former need to
be static; the latter do not.  (S'Size *is* static, by the way, if S is
a static (scalar) subtype.)  Even if an expression is non-static, a good
compiler will calculate it at compile time in *some* cases.

>This is annoying because I'd rather write
>   Page_Info_Bytes     : constant := Page_Info'Size / Byte'Size;
>than
>   Page_Info_Bytes     : Positive := Page_Info'Size / Byte'Size;

You can do this:

   Page_Info_Bytes : constant Positive := Page_Info'Size / Byte'Size;
                     ^^^^^^^^

Better yet, I suggest you define a type like Size_In_Bytes, different
from Positive, and use that.

>   -- All XXX_Size identifiers denote sizes in bytes rather than bits

I suggest you use the type system to reflect the above comment.

>   VM_Size              : constant := 2 ** 29;  -- 512 MB Virtual Memory
>   Page_Size            : constant := 2 ** 12;  -- 4 kB VM pages

And make these be of type Size_In_Bytes, too.

>   type Page_Nr is range 0.. VM_Size / Page_Size - 1; -- 2 ** 17 pages

I suggest you delete the comment "-- 2 ** 17 pages", because it is a
maintenance problem.  The whole point of saying "VM_Size / Page_Size" is
that if VM_Size or Page_Size changes, you *don't* have to change
Page_Nr.  But because of that comment, you *do* have to change the
declaration of Page_Nr.  Or, you might forget, and leave a wrong
comment, which is always worse than no comment.  (It's not so bad in
*this* case, because the related code is nearby, but still...)

- Bob




  reply	other threads:[~1996-03-22  0:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-03-22  0:00 Static expressions and using 'Size Geert Bosch
1996-03-22  0:00 ` Robert A Duff [this message]
1996-03-22  0:00 ` Norman H. Cohen
replies disabled

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