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,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,64afbf156d06bbe7 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-09-26 18:30:54 PST Path: news1.google.com!newsfeed.stanford.edu!news.uchicago.edu!news.cc.uic.edu!aanews.merit.edu!gumby.it.wmich.edu!news-out1.nntp.be!propagator2-sterling!news-in-sterling.nuthinbutnews.com!cyclone1.gnilink.net!wn14feed!worldnet.att.net!204.127.198.203!attbi_feed3!attbi.com!rwcrnsc54.POSTED!not-for-mail Message-ID: <3F74E844.1020001@comcast.net> From: "Robert I. Eachus" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.2) Gecko/20021120 Netscape/7.01 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: 'Valid, subtypes and constraint checking References: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 24.34.139.183 X-Complaints-To: abuse@comcast.net X-Trace: rwcrnsc54 1064626253 24.34.139.183 (Sat, 27 Sep 2003 01:30:53 GMT) NNTP-Posting-Date: Sat, 27 Sep 2003 01:30:53 GMT Organization: Comcast Online Date: Sat, 27 Sep 2003 01:30:53 GMT Xref: news1.google.com comp.lang.ada:43205 Date: 2003-09-27T01:30:53+00:00 List-Id: Peter Amey wrote: > My confidence has now been shaken by a test case for a compiler, as yet > unnamed, which raises constraint error for the initial assignment of an > invalid value in ExternalPort to Temp. Is this correct behaviour? If > it _is_ correct, how can you ever validate external volatile data? > > (Horrible strawman solution: do an unchecked conversion of External_Port > into Temp and then do the validity check). > > Thoughts? Define a type that matches any possible value of ExternalPort. That is the type of ExternalPort, whatever else you may think. Call it Port_Type, for now. To convert it to your internal type, you may be able to do function My_Convert is new Unchecked_Conversion(Port_Type, Internal_Type); Temp := My_Convert(ExternalPort); However, you may have to do: Temp1: Port_Type; Temp2: Internal_Type; Temp1 := ExternalPort; if Internal_Type'Valid(My_Convert(Temp1)) then Temp2 := My_Convert(Temp1); end if; Don't worry about the apparent calling of My_Convert twice. For most conversions, instances of Unchecked_Conversion generate no code. (The special cases include when the source is a slice or a packed record component, which certainly isn't happening here.) It sounds like the type you are converting to is one where the compiler assures itself that all values are in range or correct in some way. For example it could check that pointers are divisible by four for some hardware. These are the cases where you need the second construction. -- Robert I. Eachus "Quality is the Buddha. Quality is scientific reality. Quality is the goal of Art. It remains to work these concepts into a practical, down-to-earth context, and for this there is nothing more practical or down-to-earth than what I have been talking about all along...the repair of an old motorcycle." -- from Zen and the Art of Motorcycle Maintenance by Robert Pirsig