comp.lang.ada
 help / color / mirror / Atom feed
* Is there a way to see if a value is declared as a constant
@ 2021-09-13 17:08 ldries46
  2021-09-13 17:27 ` Björn Lundin
                   ` (5 more replies)
  0 siblings, 6 replies; 8+ messages in thread
From: ldries46 @ 2021-09-13 17:08 UTC (permalink / raw)


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

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

* Re: Is there a way to see if a value is declared as a constant
  2021-09-13 17:08 Is there a way to see if a value is declared as a constant ldries46
@ 2021-09-13 17:27 ` Björn Lundin
  2021-09-13 18:48 ` Jeffrey R. Carter
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Björn Lundin @ 2021-09-13 17:27 UTC (permalink / raw)


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

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

* Re: Is there a way to see if a value is declared as a constant
  2021-09-13 17:08 Is there a way to see if a value is declared as a constant ldries46
  2021-09-13 17:27 ` Björn Lundin
@ 2021-09-13 18:48 ` Jeffrey R. Carter
  2021-09-13 19:00 ` Dmitry A. Kazakov
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Jeffrey R. Carter @ 2021-09-13 18:48 UTC (permalink / raw)


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

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

* Re: Is there a way to see if a value is declared as a constant
  2021-09-13 17:08 Is there a way to see if a value is declared as a constant ldries46
  2021-09-13 17:27 ` Björn Lundin
  2021-09-13 18:48 ` Jeffrey R. Carter
@ 2021-09-13 19:00 ` Dmitry A. Kazakov
  2021-09-14  6:47 ` ldries46
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Dmitry A. Kazakov @ 2021-09-13 19:00 UTC (permalink / raw)


On 2021-09-13 19:08, ldries46 wrote:
> 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

[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

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

* Re: Is there a way to see if a value is declared as a constant
  2021-09-13 17:08 Is there a way to see if a value is declared as a constant ldries46
                   ` (2 preceding siblings ...)
  2021-09-13 19:00 ` Dmitry A. Kazakov
@ 2021-09-14  6:47 ` ldries46
  2021-09-14  6:58   ` Emmanuel Briot
  2021-09-14 23:47 ` Shark8
  2021-09-18  5:54 ` ldries46
  5 siblings, 1 reply; 8+ messages in thread
From: ldries46 @ 2021-09-14  6:47 UTC (permalink / raw)


Op 13-9-2021 om 19:08 schreef 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 := .....
>
> 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
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.

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

* Re: Is there a way to see if a value is declared as a constant
  2021-09-14  6:47 ` ldries46
@ 2021-09-14  6:58   ` Emmanuel Briot
  0 siblings, 0 replies; 8+ messages in thread
From: Emmanuel Briot @ 2021-09-14  6:58 UTC (permalink / raw)


> 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.

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

* Re: Is there a way to see if a value is declared as a constant
  2021-09-13 17:08 Is there a way to see if a value is declared as a constant ldries46
                   ` (3 preceding siblings ...)
  2021-09-14  6:47 ` ldries46
@ 2021-09-14 23:47 ` Shark8
  2021-09-18  5:54 ` ldries46
  5 siblings, 0 replies; 8+ messages in thread
From: Shark8 @ 2021-09-14 23:47 UTC (permalink / raw)


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;

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

* Re: Is there a way to see if a value is declared as a constant
  2021-09-13 17:08 Is there a way to see if a value is declared as a constant ldries46
                   ` (4 preceding siblings ...)
  2021-09-14 23:47 ` Shark8
@ 2021-09-18  5:54 ` ldries46
  5 siblings, 0 replies; 8+ messages in thread
From: ldries46 @ 2021-09-18  5:54 UTC (permalink / raw)


Op 13-9-2021 om 19:08 schreef 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 := .....
>
> 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
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

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

end of thread, other threads:[~2021-09-18  5:54 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-13 17:08 Is there a way to see if a value is declared as a constant ldries46
2021-09-13 17:27 ` Björn Lundin
2021-09-13 18:48 ` Jeffrey R. Carter
2021-09-13 19:00 ` Dmitry A. Kazakov
2021-09-14  6:47 ` ldries46
2021-09-14  6:58   ` Emmanuel Briot
2021-09-14 23:47 ` Shark8
2021-09-18  5:54 ` ldries46

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