Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you perform a check for safe type conversion?

Tags:

ada

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.

  • Is it possible to refer to the range 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.
  • Is it possible to perform this type conversion at all without subtyping while maintaining safety and not receiving a range check might fail error from gnatprove?
  • Alternatively, should I just perform no check initially, convert types and then perform 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.

like image 766
Surobaki Avatar asked Nov 11 '21 15:11

Surobaki


People also ask

How is type conversion done?

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.

Which type of conversion method is the safest?

Widening conversions (promotion) Because widening conversions are always safe, the compiler performs them silently and doesn't issue warnings.

What is type conversion with example?

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.

What is type conversion What are two ways and explain?

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.


2 Answers

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 ...
like image 56
Niklas Holsti Avatar answered Nov 04 '22 09:11

Niklas Holsti


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
like image 26
Jeffrey R. Carter Avatar answered Nov 04 '22 08:11

Jeffrey R. Carter