AdaPower Logged in as Guest
Ada Tools and Resources

Ada 95 Reference Manual
Ada Source Code Treasury
Bindings and Packages
Ada FAQ


Join >
Articles >
Ada FAQ >
Getting Started >
Home >
Books & Tutorials >
Source Treasury >
Packages for Reuse >
Latest Additions >
Ada Projects >
Press Releases >
Ada Audio / Video >
Home Pages >
Links >
Contact >
About >
Login >
Back
Non-consecutive Ranges (Tucker Taft)

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;


(c) 1998-2004 All Rights Reserved David Botton