From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,cd703a96ca51de6e X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!cyclone1.gnilink.net!spamkiller.gnilink.net!gnilink.net!trnddc04.POSTED!20ae255c!not-for-mail Newsgroups: comp.lang.ada From: Anonymous Coward Subject: Avoiding constraint checks w/ 'Base References: <1134055303.758950.308680@o13g2000cwo.googlegroups.com> <1134065313.469475.267400@g47g2000cwa.googlegroups.com> User-Agent: slrn/0.9.7.4 (Linux) Message-ID: Date: Fri, 09 Dec 2005 02:13:52 GMT NNTP-Posting-Host: 129.44.84.62 X-Complaints-To: abuse@verizon.net X-Trace: trnddc04 1134094432 129.44.84.62 (Thu, 08 Dec 2005 21:13:52 EST) NNTP-Posting-Date: Thu, 08 Dec 2005 21:13:52 EST Xref: g2news1.google.com comp.lang.ada:6783 Date: 2005-12-09T02:13:52+00:00 List-Id: In article <1134065313.469475.267400@g47g2000cwa.googlegroups.com>, Matthew Heaney wrote: > > So for example, if T is: > > type T is 1 .. 42; > > then T'Base is > > type T'Base is -42 .. 42; > > Note that built-in operators go through the base type, and T's "+" > op for example is implicitly declared as: > > function "+" (L, R : T'Base) return T'Base; > > There are no constraint checks on T'Base, so for example: > > declare > O1 : T := T'(1) + T'(2); > O2 : T'Base := T'(1) + T'(2) > begin > > then in the first assignment to O1, there is a constraint check to > ensure that the result of 1 + 2 is in the range of T, but in the > second assignment to O2, there is no check. I like the idea of avoiding constraint checks - especially when I know the result will be in range. Even if I can't be sure that the sum of the two operands is in range, it would be an advantage to be able to handle the situation without throwing an exception. So I have some questions. In your example, based on what you've said the ARM guarantees about minimal T'Base ranges, why would the initialization for O2 go unchecked, knowing that it could just as well go out of bounds? Is the 'Base attribute also code for "trust me, I know what I'm doing?" I think it's unfortunate that the 'Base does not necessarily use the full object size, so we are still forced to declare a new type, like "T_Safe" in this example: package Base_Experiment is type T is range 1..42; function "+" (L, R : T) return T; end Base_Experiment; package body Base_Experiment is function "+" (L, R : T) return T is type T_Safe is range T'First..2 * T'Last; --The following use of 'Base mitigates constraint checking? -- Sum : constant T_Safe'Base := T_Safe(L) + T_Safe(R); Return_Data : T := T'Last; begin --Do our own check if we are uncertain about the resulting --range. if Sum > T_Safe'Base(T'Last) then --[Take some corrective action here]-- --The usual crash everything without a clue: -- --raise Constraint_Error; --Give the user a reasonable chance to correct: -- --raise Our_Custom_Exception; --Probably the best answer in most cases: -- --Log_Error("Base_Experiment."+" out of bounds!"); null; else Return_Data := T(Sum); end if; return Return_Data; end "+"; end Base_Experiment;