Describing a String type in Ada

339 Views Asked by At

I have a type similar to:

type ID is new String (1 .. 7);
-- Example: 123-456

How can I specify that format in code, either with Ada or SPARK?

I was thinking about Static_Predicate, but the condition that the string must start with 3 positive integers followed by a dash followed by another set of 3 positive integers can't be described with a Static_Predicate expression.

1

There are 1 best solutions below

5
On BEST ANSWER

You have to use a Dynamic_Predicate for this:

type ID is new String (1 .. 7)
  with Dynamic_Predicate => (for all I in ID'Range =>
                               (case I is
                                   when 1 .. 3 | 5 .. 7 => ID (I) in '0' .. '9',
                                   when 4               => ID (I) in '-'));

I'm using this quite a bit myself, but I mostly make the types subtypes of String instead of actual new types.