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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,fdd685ffa59d584d X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!feeder.news-service.com!weretis.net!feeder4.news.weretis.net!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: What would be the Ada solution? Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <0aa67233-4210-483b-b35c-8e872f87cb8f@r21g2000yqd.googlegroups.com> Date: Fri, 4 Feb 2011 18:26:42 +0100 Message-ID: NNTP-Posting-Date: 04 Feb 2011 18:26:42 CET NNTP-Posting-Host: 26da813a.newsspool3.arcor-online.net X-Trace: DXC=@@0>J0LF73Q5TOT9_N5i On Fri, 4 Feb 2011 08:33:57 -0800 (PST), KK6GM wrote: > A colleague is working on some old code, written in C, that uses an > "out of range" integer value to indicate no valid value. Thus, a > default value (in this case 0x7FFFFFFF) means no value has been > entered. All code that uses any values should check for this no-value > and act accordingly, but of course not all the code actually does > that, and there are odd cases where the no-value value gets processed > as a valid value and then Bad Things Happen. > > I'm curious what the Ada approach to this issue would be, the issue > being to differentiate between valid and invalid values, and to catch > (compile time or run time) any manipulation of an invalid value as if > it were a valid value. Ada solution would be to declare the integer type of the valid range: type ADC_16_Bit is range 0..2**16 - 1; The compiler checks dynamically and, where possible, statically that the value is always valid. When values read from the hardware some bit patterns may indicate errors. In such cases you can declare the full range of possible values and a subrange of the valid ones: type ADC_Word is mod 2**16; -- 2 octets as read from the station Conversion_Error : constant ADC_Word := 16#FFFF#; Short_Circuit_Error : constant ADC_Word := 16#EFFF#; ... subtype Voltage is ADC_Word range 0..16#7FFF#; -- 0=-10V, 7FFF=+10V -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de