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=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Date: 27 Sep 91 14:13:00 GMT From: cis.ohio-state.edu!zaphod.mps.ohio-state.edu!rpi!bu.edu!inmet!stt@ucbvax. Berkeley.EDU Subject: Re: Untyped "constant"? Message-ID: <20600118@inmet> List-Id: Re: Untyped "constant"? by gralia@ddsdx2.jhuapl.edu > I have a question about Ada. It has to do with "named numbers" and > their subsequent use. > > 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; This is not legal Ada, since the type of integer'last+5 is integer, not universal-integer. This can be made legal as follows: X : constant := Integer'POS(Integer'LAST) + 5; This works because int-type'POS returns the value of its operand converted to univ-integer type. > begin > for j in 1..X loop This will not work in general, because when both bounds can be interpreted as universal integer (as above), the type of J will be STANDARD.INTEGER. Therefore, you should get CONSTRAINT_ERROR on this line. > 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? No, it is not legal as written (see above for correction). > Thanks, > Mars Gralia > > > > DISCUSSION > > 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; This is also illegal, because, again, the type on the right is Integer, not Long_Integer. It could be made legal as follows: X : constant Long_Integer := Long_Integer(Integer'LAST) + 5; In any case, *all* compilers should raise CONSTRAINT_ERROR if you say "for J in 1..X" when X is a universal-integer > Integer'LAST. By the way, "X : constant := ..." must *never* raise constraint error on the assigment, though some subexpression on the right hand side might raise constraint error, presuming it is inside a 'POS (as above). All static univ-integer calculations must be performed to arbitrary range (no overflow allowed). > I would prefer the program given above, because it would appear to be > more portable. By using 'POS, you can make the declaration of X work without constraint error, but you will still get a constraint error on the loop unless you specify a type for J that includes the low and high bound. Furthermore, there exist compilers where Integer'LAST is equal to SYSTEM.MAX_INT, and in that case you are out of luck. S. Tucker Taft stt@inmet.inmet.com Intermetrics, Inc. Cambridge, MA 01238