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 .. 9that defines theDigittype without specifically statingIF InputInteger IN Digit? Because there's no subtyping,INwill 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 asDigitRange. - 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'Validas 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.