comp.lang.ada
 help / color / mirror / Atom feed
From: reason67@my-deja.com
Subject: Re: Questions about Unchecked_Conversion
Date: 1999/12/15
Date: 1999-12-15T00:00:00+00:00	[thread overview]
Message-ID: <8396b3$p6p$1@nnrp1.deja.com> (raw)
In-Reply-To: 19991215201559.23735.qmail@web218.mail.yahoo.com

In article <19991215201559.23735.qmail@web218.mail.yahoo.com>,
  comp.lang.ada@ada.eu.org wrote:
> 1. Could someone please explain a few things about Unchecked
Conversion
>    and what it really does for me? What if I dont have it there?

NOTE: I am assuming Ada83 as you used Ada83 names.. If it is 95, my
explainations still apply but my examples could be written better.

Unchecked conversion is just as the name says. It converts data from one
type to another without doing any error checking. So if you had:

type Traffic_Light_Type is (Red, Yellow, Green);
for Traffic_Light_Type use (Red => 15, Yellow => 45, Green => 60);
for Traffic_Light_Type'Size use 16;

type Word_Type is range 0 .. 65536;
for Word_Type'Size use 16;

function To_Traffic_Light_Type is new Unchecked_Conversion
   (Source => Word_Type,
    Target => Traffic_Light_Type);

Word          : Word_Type;
Traffic_Light : Traffic_Light_Type;
...
begin
...

   Traffic_Light := To_Traffic_Light_Type (Word);
...
end;

The conversion from Word to Traffic light is done unconditionally. If
Word does not equal 15, 45, or 60, the conversion still happens with no
error. Upon use of Traffic_Light, a Constraint_Error would be raised.

If you do not use Unchecked_Conversion, you have to do the conversion
manually. In my example, that is fairly easy. But if converting between
a 32 bit Float and an 4 byte array, that is far more complicated.
Conversions such as that can not be done through type casting.

> 2. What is the difference (as far as unchecked_conversion is
concerned)
>
>    between the following?
>
>    a. FUNCTION ABC(A, B);
>    b. FUNCTION KLM IS NEW Unchecked_Conversion(A, B);

ABC has a different spec then KLM. KLM only takes A as a parameter and
returns B.

>    Could the KLM function be defined the same as the ABC function
> above,
>    but in the body of the KLM function be placed a WITH
> Unchecked_Conversion;
>    statement? Would this have the same effect?

If the specs matched, yes. You could hide the use of Unchecked
conversion in the body without changing the functionality at all.

> 3. What happens if we place the statement With Unchecked_Conversion;
>    somewhere in the top of our program? Does this mean that from then
>    on, and for the whole pgm, no conversion exception is raised
>    anywhere?

If you placed the with of unchecked_conversion before the package, it is
visible thoughout the package. Withing in Unchecked_Conversions has
absolutely no effect other than giving you package visibility to
Unchecked_Conversion. Any type casting you do would still be subject to
Ada type checking. The only thing that would be exempt would be the
specific calls to instantiations of unchecked_conversion.
---
Jeffrey S. Blatt


Sent via Deja.com http://www.deja.com/
Before you buy.




  reply	other threads:[~1999-12-15  0:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-12-15  0:00 Questions about Unchecked_Conversion MaryAnn Atkinson
1999-12-15  0:00 ` reason67 [this message]
1999-12-15  0:00 ` Matthew Heaney
1999-12-16  0:00 ` Charles H. Sampson
1999-12-16  0:00   ` Matthew Heaney
1999-12-17  0:00     ` reason67
1999-12-18  0:00     ` Charles H. Sampson
replies disabled

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