comp.lang.ada
 help / color / mirror / Atom feed
From: "Robert I. Eachus" <rieachus@comcast.net>
Subject: Re: 'Valid, subtypes and constraint checking
Date: Sat, 27 Sep 2003 01:30:53 GMT
Date: 2003-09-27T01:30:53+00:00	[thread overview]
Message-ID: <3F74E844.1020001@comcast.net> (raw)
In-Reply-To: bl18e1$6ot2h$1@ID-69815.news.uni-berlin.de

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




  parent reply	other threads:[~2003-09-27  1:30 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-09-26 11:41 'Valid, subtypes and constraint checking Peter Amey
2003-09-26 13:13 ` Jean-Pierre Rosen
2003-09-26 18:18 ` Jeffrey Carter
2003-09-26 21:48   ` Chad R. Meiners
2003-09-27  1:30 ` Robert I. Eachus [this message]
2003-09-29  7:27   ` 'Valid, subtypes and constraint checking - Thanks Peter Amey
2003-09-30  2:58     ` Robert I. Eachus
2003-09-30 12:45       ` Marin David Condic
2003-09-30 21:30         ` Robert I. Eachus
2003-10-01 12:44           ` Marin David Condic
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox