From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-0.9 required=3.0 tests=BAYES_00,FROM_ADDR_WS autolearn=no autolearn_force=no version=3.4.5-pre1 Date: 24 Sep 91 21:22:10 GMT From: bywater!arnor!watson!prener.watson.ibm.com!ncohen@uunet.uu.net (Norman H . Cohen) Subject: Re: Untyped "constant"? Message-ID: <1991Sep24.212210.58452@watson.ibm.com> List-Id: In article <1991Sep23.164446.13412@aplcen.apl.jhu.edu>, gralia@ddsdx2.jhuapl.edu (Mars J. Gralia) writes: |> a) Suppose I have a compiler/RTE in which integer'last is 32_767. |> b) And suppose the system has a long_integer type in which |> long_integer'last = 2_147_483_647. |> c) And suppose I have the program: |> procedure Named1 is |> X : constant := integer'last+5; |> begin |> for j in 1..X loop |> null; |> end loop; |> end Named1; |> |> d) Finally, suppose the "loop" variables are really used. E.g., by |> disabling optimization when I invoke the compiler. |> |> |> Then: is this a legal program or not? As Dave Collard points out, Integer'Last+5 is of type Integer, and the initial-value expression for a named number must be of type universal_integer, so this is not legal. However, we can get around this technicality by rewriting the initial-value expression as Integer'Pos(Integer'Last)+5 since the 'Pos attribute yields a universal_integer result. However, RM 3.6.1(2), when deciphered, states that a discrete range in which each bound is either a named number, an integer literal, or an attribute with a universal_integer value has each bound implicitly converted to type Integer. Thus 1 .. X is equivalent to Integer(1) .. Integer(X). Since X has a value greater than Integer'Last, the conversion in the upper bound raises Constraint_Error. |> Some compilers, admittedly archaic or for very small computers, |> will raise a Constraint_Error, thereby forcing me to say: |> X : constant long_integer := integer'last+5; |> |> I would prefer the program given above, because it would appear to be |> more portable. Well, it's portable in the sense that it is guaranteed to raise Constraint_Error (except when the loop is optimized away, in which case the implicit conversion that would have raised Constraint_Error can be eliminated pursuant to RM 11.6(7)). A more useful form of portability would come from defining your own integer type with the required range: procedure Named1 is type Loop_Index_Type is range 1 .. Integer'Last+5; begin for J in Loop_Index_Type loop null; end loop; end Named1; Implementations for which Integer'Last = System.Max_Int will reject this at compile time. Implementations for which System.Max_Int >= Integer'Last+5 are guaranteed to execute without an exception.