From: Adrian Hoe on
Hi,

Perhaps I have overlook the repository. What is the best (safest) way
to convert an Integer to Short_Short_Integer?

1. Using Ada.Unchecked_Conversion raises warning types of unchecked
conversion have different size.

2. Using a custom function such as:

function To_Short_Short_Integer ( I : Integer ) return
SHort_Short_Integer
is
function Convert_To_Short_Short_Integer is new
Ada.Unchecked_Conversion ( Integer, Short_Short_Integer ) ;
begin
If I >= Short_Short_Integer'First and I <= Short_Short_Integer'Last
then
return Convert_To_Short_Short_Integer ( I );
else
raise Range_Error;
end if;
end To_Short_Short_Integer;

Again, the Unchecked_Conversion will raise the same warning in (1).

Is (2) the best (safest) way to do the conversion? Is there any better
method?

Thanks.
--
Adrian Hoe
From: Adrian Hoe on
On Jun 11, 9:17 am, Adrian Hoe <aby...(a)gmail.com> wrote:
> Hi,
>
> Perhaps I have overlook the repository. What is the best (safest) way
> to convert an Integer to Short_Short_Integer?
>
> 1. Using Ada.Unchecked_Conversion raises warning types of unchecked
> conversion have different size.
>
> 2. Using a custom function such as:
>
> function To_Short_Short_Integer ( I : Integer ) return
> SHort_Short_Integer
> is
>    function Convert_To_Short_Short_Integer is new
>       Ada.Unchecked_Conversion ( Integer, Short_Short_Integer ) ;
> begin
>    If I >= Short_Short_Integer'First and I <= Short_Short_Integer'Last
> then
>       return Convert_To_Short_Short_Integer ( I );
>    else
>       raise Range_Error;
>    end if;
> end To_Short_Short_Integer;
>
> Again, the Unchecked_Conversion will raise the same warning in (1).
>
> Is (2) the best (safest) way to do the conversion? Is there any better
> method?
>
> Thanks.
> --
> Adrian Hoe

Sorry, I forgot to convert to Short_Short_Integer'First and
Short_Short_Integer'Lastt to Integer.

--
Adrian Hoe
From: Adrian Hoe on
On Jun 11, 9:17 am, Adrian Hoe <aby...(a)gmail.com> wrote:
> Hi,
>
> Perhaps I have overlook the repository. What is the best (safest) way
> to convert an Integer to Short_Short_Integer?
>
> 1. Using Ada.Unchecked_Conversion raises warning types of unchecked
> conversion have different size.
>
> 2. Using a custom function such as:
>
> function To_Short_Short_Integer ( I : Integer ) return
> SHort_Short_Integer
> is
>    function Convert_To_Short_Short_Integer is new
>       Ada.Unchecked_Conversion ( Integer, Short_Short_Integer ) ;
> begin
>    If I >= Short_Short_Integer'First and I <= Short_Short_Integer'Last
> then
>       return Convert_To_Short_Short_Integer ( I );
>    else
>       raise Range_Error;
>    end if;
> end To_Short_Short_Integer;
>
> Again, the Unchecked_Conversion will raise the same warning in (1).
>
> Is (2) the best (safest) way to do the conversion? Is there any better
> method?
>
> Thanks.
> --
> Adrian Hoe

Sorry, I forgot to convert Short_Short_Integer'First and
Short_Short_Integer'Lastt to Integer.
--
Adrian Hoe
From: Adrian Hoe on
On Jun 11, 9:17 am, Adrian Hoe <aby...(a)gmail.com> wrote:
> Hi,
>
> Perhaps I have overlook the repository. What is the best (safest) way
> to convert an Integer to Short_Short_Integer?
>
> 1. Using Ada.Unchecked_Conversion raises warning types of unchecked
> conversion have different size.
>
> 2. Using a custom function such as:
>
> function To_Short_Short_Integer ( I : Integer ) return
> SHort_Short_Integer
> is
>    function Convert_To_Short_Short_Integer is new
>       Ada.Unchecked_Conversion ( Integer, Short_Short_Integer ) ;
> begin
>    If I >= Short_Short_Integer'First and I <= Short_Short_Integer'Last
> then
>       return Convert_To_Short_Short_Integer ( I );
>    else
>       raise Range_Error;
>    end if;
> end To_Short_Short_Integer;
>
> Again, the Unchecked_Conversion will raise the same warning in (1).
>
> Is (2) the best (safest) way to do the conversion? Is there any better
> method?
>
> Thanks.
> --
> Adrian Hoe

Sorry, I forgot to convert Short_Short_Integer'First and
Short_Short_Integer'Last to Integer.
--
Adrian Hoe
From: Jeffrey R. Carter on
You convert between numeric types by using a type conversion:

I : Integer := Integer'Last;
S : Short_Short_Integer; -- Not portable
....
S := Short_Short_Integer (I);

If the value of I is not within the range of Short_Short_Integer, the conversion
will raise Constraint_Error.

This is basic Ada, and if it's not what you want, then you should use another
term than "convert" (and a lengthy explanation of what you're trying to achieve
and why is probably in order).

--
Jeff Carter
"In the frozen land of Nador they were forced to
eat Robin's minstrels, and there was much rejoicing."
Monty Python & the Holy Grail
70