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

    From ldries46@21:1/5 to All on Mon Sep 13 19:08:59 2021
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From =?UTF-8?Q?Bj=c3=b6rn_Lundin?=@21:1/5 to All on Mon Sep 13 19:27:42 2021
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jeffrey R. Carter@21:1/5 to All on Mon Sep 13 20:48:40 2021
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dmitry A. Kazakov@21:1/5 to All on Mon Sep 13 21:00:45 2021
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Emmanuel Briot@21:1/5 to All on Mon Sep 13 23:58:18 2021
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From ldries46@21:1/5 to All on Tue Sep 14 08:47:17 2021
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Shark8@21:1/5 to All on Tue Sep 14 16:47:21 2021
    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;

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From ldries46@21:1/5 to All on Sat Sep 18 07:54:09 2021
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From ldries46@21:1/5 to All on Thu Sep 23 10:01:15 2021
    This is a multi-part message in MIME format.
    Op 18-9-2021 om 7:54 schreef ldries46:
    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
    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".

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
    <div class="moz-cite-prefix">Op 18-9-2021 om 7:54 schreef ldries46:<br>
    </div>
    <blockquote type="cite"
    cite="mid:nnd$32625521$73c3d8da@cb0786dc2fdce31d">Op 13-9-2021 om
    19:08 schreef ldries46:
    <br>
    <blockquote type="cite">I have a set of constants that need a
    different name each for readability. It may not be an array.
    <br>
    For instance:
    <br>
    C1 : constant record_item := .....
    <br>
    C2 : constant record_item := .....
    <br>
    C3 : constant record_item := .....
    <br>
    C4 : constant record_item := .....
    <br>
    C5 : constant record_item := .....
    <br>
    <br>
    Now in a procedure or a function I have to use one of these
    constants
    <br>
    for instance:
    <br>
    <br>
    function X(C : record_item) return record_Item is
    <br>
       RI : record_item;
    <br>
    begin
    <br>
       ..
    <br>
       ..
    <br>
       RI := C -- This C may only be one of the five constants and
    not another record_item
    <br>
       ..
    <br>
       ..
    <br>
        return RI;
    <br>
    end X;
    <br>
    <br>
    In what way do I test if C is a constant and not another record
    Item
    <br>
    </blockquote>
    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
    <br>
    </blockquote>
    The solution that finally worked is:<br>
    Create a new record<br>
    <i>type C_record_item is record </i><i><br>
    </i><i>   Item : record_Item;</i><i><br>
    </i><i>   const : boolean := true;</i><i><br>
    </i><i>end;</i><i><br>
    </i>then<br>
    <i>C1 : constant C_record_item := .....
    </i><i><br>
    </i><i>C2 : constant C_record_item := .....
    </i><i><br>
    </i><i>C3 : constant C_record_item := .....
    </i><i><br>
    </i><i>C4 : constant C_record_item := .....
    </i><i><br>
    </i><i>C5 : constant C_record_item := .....
    </i><i><br>
    </i><i><br>
    </i><i>function X(C : C_record_item) return record_Item is
    </i><i><br>
    </i><i>   RI : record_item;
    </i><i><br>
    </i><i>begin
    </i><i><br>
    </i><i>   ..
    </i><i><br>
    </i><i>   ..
    </i><i><br>
    </i><i>   RI := C.item; -- This C may only be one of the five
    constants and not another record_item
    </i><i><br>
    </i><i>   ..
    </i><i><br>
    </i><i>   ..
    </i><i><br>
    </i><i>    return RI;
    </i><i><br>
    </i><i>end X;
    </i><i><br>
    </i><i>
    </i>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".<br>
    </body>
    </html>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)