Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is there a way to see if a value is declared as a constant

99 views
Skip to first unread message

ldries46

unread,
Sep 13, 2021, 1:10:03 PM9/13/21
to
I have a set of constants that need a different name each for
readability. It may not be an array.
For instance:
C1 : constant record_item := .....
C2 : constant record_item := .....
C3 : constant record_item := .....
C4 : constant record_item := .....
C5 : constant record_item := .....

Now in a procedure or a function I have to use one of these constants
for instance:

function X(C : record_item) return record_Item is
   RI : record_item;
begin
   ..
   ..
   RI := C -- This C may only be one of the five constants and not
another record_item
   ..
   ..
    return RI;
end X;

In what way do I test if C is a constant and not another record Item

Björn Lundin

unread,
Sep 13, 2021, 1:27:44 PM9/13/21
to
Den 2021-09-13 kl. 19:08, skrev ldries46:
> I have a set of constants that need a different name each for
> readability. It may not be an array.
> For instance:
> C1 : constant record_item := .....
> C2 : constant record_item := .....
> C3 : constant record_item := .....
> C4 : constant record_item := .....
> C5 : constant record_item := .....


Two approaches - but both uses arrays.
You don't say why (more that readability) you don't want array.

If it is because integer idexing you can skip that and
put the values in an array, using a
readable index type

eg (for placing bets at Betfair - football/soccer

type Bet_Market_Type is (Match_Odds,
Correct_Score,
Half_Time_Score,
Hat_Tricked_Scored,
Penalty_Taken,
Sending_Off);

Market : array (Bet_Market_Type'range) of record_item := (...);


use as

Market(Correct_Score) := ...



or
put the values in an array.
Let constants rename item in array

use constants in code

check RI in array




>
> Now in a procedure or a function I have to use one of these constants
> for instance:
>
> function X(C : record_item) return record_Item is
>    RI : record_item;
> begin
>    ..
>    ..
>    RI := C -- This C may only be one of the five constants and not
> another record_item
>    ..
>    ..
>     return RI;
> end X;
>
> In what way do I test if C is a constant and not another record Item


--
Björn

Jeffrey R. Carter

unread,
Sep 13, 2021, 2:48:44 PM9/13/21
to
On 9/13/21 7:08 PM, ldries46 wrote:
>
> In what way do I test if C is a constant and not another record Item

function X (C : in Record_Item) return Record_Item with
Pre => C = C1 or C = C2 or C = C3 or C = C4 or C = C5;

Of course, if you have a lot of constants with more descriptive names, the
precondition will be hard to read. If you had a constant array it would be more
readable:

Pre => (for some A of Allowed_Values => C = A);

--
Jeff Carter
"You've got the brain of a four-year-old boy,
and I bet he was glad to get rid of it."
Horse Feathers
47

Dmitry A. Kazakov

unread,
Sep 13, 2021, 3:00:49 PM9/13/21
to
[The requirement makes no sense and suggests design error]

type Record_Item is tagged record
I : Integer;
end record;
...

type Dedicated_Record_Item is new Record_Item with null record;
C1 : constant Dedicated_Record_Item := (I => 1);
C2 : constant Dedicated_Record_Item := (I => 2);
C3 : constant Dedicated_Record_Item := (I => 3);
C4 : constant Dedicated_Record_Item := (I => 4);
C5 : constant Dedicated_Record_Item := (I => 5);

function X (C : Dedicated_Record_Item) return Record_Item is
begin
return RI : Record_Item := Record_Item (C);
end X;

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

ldries46

unread,
Sep 14, 2021, 2:47:23 AM9/14/21
to
Op 13-9-2021 om 19:08 schreef ldries46:
The reason I want this construction is that it is part of a reusable
package where one of the functions may only use a constant.
That function is a kind of initiation function that the end user should
use in his/her end program.
I hoped there would be an attribute that shows if the item is a constant
and that I want to be be tested. The only simple way I know at this
moment is adding a boolean to the record that can be tested but that
means that that boolean may not be reached by the the program  that uses
the package, so only that boolean should be (limited) private.
The reason that I do not want to use an array is that however the record
and the functions used on these records are all the same, the physical
items  are different. Not using an array means that the readability of
the of the package will be better.

Emmanuel Briot

unread,
Sep 14, 2021, 2:58:20 AM9/14/21
to
> items are different. Not using an array means that the readability of
> the of the package will be better.

It won't, really. If you follow Bjorn's suggestion of using an enumeration type (user visible)
and an array (in the body of your package, to get the associated constant), you have a very
readable package, where users can only use one of your enumeration literals.

With the approach you are trying to put in place, users could define their own constants that
doesn't match the ones you provide. They would still be "constant", so even if it was possible
to write a test like you were looking for, that test would pass.

Shark8

unread,
Sep 14, 2021, 7:47:23 PM9/14/21
to
When you start fighting the language, it's usually an indicator that your design needs changing.
Try using the things Ada does (e.g. types) to model your problem-space.

Package Example is
Type Whatever is private;
I, J, K : Constant Whatever;
Private
Type Whatever is new Integer;
Type Constants is Array (Positive range <>) of Whatever
with Dynamic_Predicate =>
(for all Index_1 in Constants'First..Natural'Pred(Constants'Last) =>
(for all Index_2 in Positive'Succ(Index_1)..Constants'Last =>
Constants(Index_1) /= Constants(Index_2)
)
);

Constant_Store : Constant Constants:= (1,7,3,11,77,2,7);

I : Constant Whatever:= Constant_Store(4);
J : Constant Whatever:= Constant_Store(2);
K : Constant Whatever:= Constant_Store(5);
End Example;

ldries46

unread,
Sep 18, 2021, 1:56:03 AM9/18/21
to
Op 13-9-2021 om 19:08 schreef ldries46:
In the meantime I have added a boolean to the record "record_item"in
which true means this is a constant and false  this not a constant then
in the function X  I raise an exception if that boolean if false

ldries46

unread,
Sep 23, 2021, 4:02:03 AM9/23/21
to
Op 18-9-2021 om 7:54 schreef ldries46:
The solution that finally worked is:
Create a new record
type C_record_item is record
   Item : record_Item;
   const : boolean := true;
end;
then
C1 : constant C_record_item := .....
C2 : constant C_record_item := .....
C3 : constant C_record_item := .....
C4 : constant C_record_item := .....
C5 : constant C_record_item := .....

function X(C : C_record_item) return record_Item is
   RI : record_item;
begin
   ..
   ..
   RI := C.item; -- This C may only be one of the five constants and not another record_item
   ..
   ..
    return RI;
end X;
Now You have the possibility to use a function or a procedure in which you are forced to use the constant or else you get an error message . For instance if the function X is a "function New_Item".
0 new messages