[comp.lang.ada] Sets in Ada

sommar@enea.se (Erland Sommarskog) (12/04/88)

Ray Trent (trent@unix.sri.com) writes:
>Another change I'd like to see. (upward compatible this time) Allow:
>
>  if A in B | C | D | Q | Y | N then
>    ...
>  end if;
>
>I.e. an extension of the membership idea. It's ridiculous to me that

Although it would be nice to have in the language as such, it is fairly 
easy to write your own generic Set package. I include some excerpts from 
my own:

   Generic
      Type Element is (<>);
   Package Set_handler is
   
   Type Set_type is private;
   Empty_set : Constant Set_type;
   Type Element_array is array(integer range <>) of Element;

   Function Set(Elem : Element) return Set_type;   
   Function Set(Elems : Element_array) return Set_type;
   -- A set of all compontens in Elems.
   Function Set(From, To : Element) return Set_type; 
   -- A set of the range From..To.
...
   Function In_set(Elem : Element; A : Set_type) return boolean; 
   
Instantiate that on your favourite enumarate and you can write:

     IF In_set(A, Set((B, C, D, Y, Z))) THEN 
     
Somewhat obscure, agreed. (It would have been nice here if the IN
operator had been overloadable.)
-- 
Erland Sommarskog
ENEA Data, Stockholm
sommar@enea.se
"Frequently, unexpected errors are entirely unpredictable" - Digital Equipment