Ada: Importing the inequality operator "/="

Viewed 135

I don't want to use an entire package, but I do want to import a few features, such as the "/=" operator. I know that renames allows me to do this with most functions, but with the inequality operator I get the error, explicit definition of inequality not allowed. How do I import this operator without raising the error?

package Integer_Maps is new Ada.Containers.Ordered_Maps
(
 Key_Type => Integer,
 Element_Type => Integer
);

-- the next line fails!
function "/=" ( Left, Right: Integer_Maps.Cursor ) return Boolean
   renames Integer_Maps."/=";
2 Answers

You could do a use type Integer_Maps.Cursor; which gives visibility of the operators on the type.

For the container cursors, it might also be practical to do a use all type Integer_Maps.Cursor; which gives visibility of all the primitive operations on the type, like Key and Element.

I usually put use type and use all type clauses in the innermost enclosing scope (i.e. inside subprograms) where they're needed, like so:

   procedure Foo is
       use all type Integer_Maps.Cursor; 
   begin
       for Cursor in My_Map.Iterate loop
          Ada.Text_IO.Put_Line
            (Integer'Image(Key(Cursor)) & " ->" & Integer'Image(Element(Cursor)));
       end loop;
   end Foo;

You don't! not directly, at any rate. Renaming the equality operator "=", and you get inequality for free.

-- this line succeeds!
function "=" ( Left, Right: Integer_Maps.Cursor ) return Boolean
   renames Integer_Maps."=";

This is similar to overriding the operators. See ARM 6.6, in particular Static Semantics.

Related