Can anyone please make me clear about the use of unchecked conversion in the Ada language.I had tried the pdf and net but all doesn't give me a clear picture to me.
Now I have a small piece of code shown below:
subtype Element4_Range is integer range 1..4;
subtype Element3_Range is integer range 1..3;
subtype Myarr_Range is integer range 1..10;
type Myarr3_Type is array (Myarr_Range) of Element3_Range;
type Myarr4_Type is array (Myarr_Range) of Element4_Range;
Myarr3 : Myarr3_Type;
Myarr4 : Myarr4_Type := (1, 2, 3, 3, 1, 3, 2, 1, 2, 1);
Count_1 : Integer := 0;
Count_2 : Integer := 0;
Count_3 : Integer := 0;
*function To_Myarr3 is new Unchecked_Conversion(Myarr4_type, Myarr3_type);*
Now my doubt here is what does the function Myarr3 exactly do?
I had an coworker once who instisted that
unchecked_conversion
should have been named "Unchcked_Copy" instead. All it does is copy an object of one type into an object of another type.So your
To_Myarr3
routine will accept as a parameter an array of type Myarr4, pretend it is an array of type Myarr3, and then copy every element in it into the left-hand side of your expression.If you want to change your view of an object from one type to another without copying the whole darn thing around, you can instead use an Unchecked_Conversion on access types for them (so you are only copying the pointer to them). Another method is using
for object_name'address use at
to overlay one on the other (however, they may both get initialized, which can be bad). But really the best way is to design your system's types so that you never have to use Unchecked_Conversion.