Non-consecutive Ranges


There is nothing like non-consecutive ranges built into the language.
However, you could define a private type and a "convert_from_int"
function which did an arbitrarily complicated test before
agreeing to convert the integer to a value of the private type.
You could "inline" this function to get roughly the same
performance as if the capability were supported directly
through some special syntax.

If you had a general requirement for types like this, you
could go so far as to write a generic package to be used
for defining such "holey" types, with generic formal parameters
indicating the gap(s) in the range.  E.g.:

generic
    type Underlying_Type is (<>);
    First, Gap_First, Gap_Last, Last : Underlying_Type;
package Holey_Types is
    type Holey is private;
    function Make_Holey(Val : Underlying_Type) return Holey;
    function Val(H : Holey) return Underlying_Type;
private
    pragma Inline(Make_Holey, Val);
    type Holey is new Underlying_Type range First .. Last;
end Holey_Types;

package body Holey_Types is
    function Make_Holey(Val : Underlying_Type) return Holey is
    begin
        if Val in Gap_First .. Gap_Last then
            raise Constraint_Error;
        end if;
        return Holey(Val);  -- Constraint_Error if not in First .. Last
    end Make_Holey;
    function Val(H : Holey) return Underlying_Type is
    begin
        return Underlying_Type(H);
    end Val;
end Holey_Types;

Contributed by: Tucker Taft
Contributed on: May 14, 1999
License: Public Domain
Back