To preface: I'm programming in the newest version (as of writing) of SPARK Ada with GNAT Community.
I've been looking over the internet for a simple solution for this but all results seem to point to the same answer that isn't working for me. I have a new type Digit
defined as TYPE Digit IS new Integer range 0 .. 9
. I'd like to safely convert Integer
to Digit
. For the sake of this conversion I've also created a range DigitRange
defined as TYPE DigitRange IS range 0 .. 9
. I'm attempting to perform this conversion by checking whether or not Digit
is in the range (IF InputInteger IN DigitRange
) but this raises an incompatible types compilation error.
0 .. 9
that defines the Digit
type without specifically stating IF InputInteger IN Digit
? Because there's no subtyping, IN
will not produce helpful results and that if statement would be invalid. I'd like to explicitly write in code that my conversion isn't performed by checking compatibility with an arbitrary variable such as DigitRange
.gnatprove
?Output'Valid
as a last resort? As far as I understand, this would still give me a range check might fail for the type conversion itself.I'm aware that the more ideal solution to achieve this is using subtypes but I'm not permitted to do that.
Thank you for your answers.
Also known as 'automatic type conversion'. Done by the compiler on its own, without any external trigger from the user. Generally takes place when in an expression more than one data type is present.
Widening conversions (promotion) Because widening conversions are always safe, the compiler performs them silently and doesn't issue warnings.
In computer science, type conversion, type casting, type coercion, and type juggling are different ways of changing an expression from one data type to another. An example would be the conversion of an integer value into a floating point value or its textual representation as a string, and vice versa.
In computer science, type conversion or typecasting refers to changing an entity of one datatype into another. There are two types of conversion: implicit and explicit. The term for implicit type conversion is coercion. Explicit type conversion in some specific way is known as casting.
Applying Integer'Pos to the Integer value "converts" it to a "universal integer", which you can then test for inclusion in the range of any integer type:
X : Integer;
...
if Integer'Pos(X) in Digit then ...
As with many things in Ada, there are multiple ways to do this in Ada, and while the use of 'Pos
is an excellent solution, it may be useful to see some others.
Since Digit
is derived from Integer
, their base types have the same range, so you can write
Digit'Base (X) in Digit
You can also define a subtype of Integer with the desired range and use it for the comparison
subtype Digit_Range is Integer range Digit'First .. Digit'Last;
X in Digit_Range
The right side of [not] in
can also be a subtype definition, so you can also use
X in Integer range Digit'First .. Digit'Last
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With