comp.lang.ada
 help / color / mirror / Atom feed
* Pb with use of redefined "=" operator
@ 1998-11-03  0:00 Fran�oise & Herv� BITTEUR
  1998-11-04  0:00 ` Mats Weber
                   ` (2 more replies)
  0 siblings, 3 replies; 20+ messages in thread
From: Fran�oise & Herv� BITTEUR @ 1998-11-03  0:00 UTC (permalink / raw)


I just run in a problem when combining :
- Ada.Strings.Bounded where '=' operator is redefined,
- A so far correct generic package (old Ada83 code) importing a private
(non-limited) type.

The problem is that any instantiation of the generic package will use
the "predefined" equality operator provided with any non-limited type,
rather than the specific definition related to Ada.Strings.Bounded.
Which leads to unexpected results on some occasions.

I have attached a very simplified example to illustrate the situation.
An obvious fix is to modify the spec of the generic package to
explicitly import a definition of '=' :
   with function "=" (L, R : in Item) return Boolean is <>;
And the redefined version if any (or the predefined one if none) will
then be used.

But what puzzles me is that :
1/ No warning is raised by the compiler (how could it be otherwise ?)
2/ Correct code (till now) has silently got broken.

Would you thus recommend that the line above ("with function "=" ...) be
systematically added to each and every existing generic package using
non-limited parameters ?

Thanks for any advice.

--Herv� BITTEUR

hbitteur@club-internet.fr

------ Example - begin
-----------------------------------------------------------

package Ints is
   type Int is new Integer;
   function "=" (Left, Right : Int) return Boolean;
end Ints;

with Ada.Text_IO;   use Ada.Text_IO;
package body Ints is
   function "=" (Left, Right : Int) return Boolean is
   begin
      Put_Line ("Ints. Function '=' is called");
      return True;  -- On purpose, to recognise the call.
   end "=";
end Ints;

generic
   type Item is private;
   Name : in String;
   with function "=" (L, R : in Item) return Boolean is <>;-- Added Line
!!!!!
package Gen_Test is
   procedure Foo (Left, Right : Item);
end Gen_Test;

with Ada.Text_IO;   use Ada.Text_IO;
package body Gen_Test is
   procedure Foo (Left, Right : Item) is
   begin
      if Left = Right then                   -- Which "=" operation ?
         Put_Line (Name & "Gen_Test. Items are equal");
      else
         Put_Line (Name & "Gen_Test. Items are not equal");
      end if;
   end Foo;
end Gen_Test;

with Ints;           use Ints;
with Gen_Test;
with Ada.Text_IO;    use Ada.Text_IO;
procedure Test is
   package Int_Test is new Gen_Test (Int, "Int. ");
   package Integer_Test is new Gen_Test (Integer, "Integer. ");
   I : Int := 1;
   J : Int := 2;
begin
   if I = J then
      Put_Line ("Test. Ints are equal");
   else
      Put_Line ("Test. Ints are not equal");
   end if;
   Int_Test.Foo (I, J);                         -- Ints."="
   Integer_Test.Foo (Integer(I), Integer(J));   -- Predefined "="
end Test;

------ Example - end
-------------------------------------------------------------

Here are the results using GNAT

a/ without the added line:
Ints. Function '=' is called
Test. Ints are equal
Int. Gen_Test. Items are not equal
Integer. Gen_Test. Items are not equal

b/ with the added line:
Ints. Function '=' is called
Test. Ints are equal
Ints. Function '=' is called
Int. Gen_Test. Items are equal
Integer. Gen_Test. Items are not equal




^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~1998-11-10  0:00 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-11-03  0:00 Pb with use of redefined "=" operator Fran�oise & Herv� BITTEUR
1998-11-04  0:00 ` Mats Weber
1998-11-04  0:00   ` Fran�oise & Herv� BITTEUR
1998-11-04  0:00 ` dewarr
1998-11-04  0:00   ` Tucker Taft
1998-11-04  0:00 ` Matthew Heaney
1998-11-04  0:00   ` Mats Weber
1998-11-05  0:00     ` Matthew Heaney
1998-11-05  0:00       ` dewarr
1998-11-05  0:00         ` Matthew Heaney
1998-11-06  0:00           ` dewarr
1998-11-09  0:00             ` Robert A Duff
1998-11-10  0:00               ` dennison
1998-11-10  0:00                 ` Robert A Duff
1998-11-05  0:00       ` Mats Weber
1998-11-05  0:00         ` Matthew Heaney
1998-11-09  0:00           ` Robert A Duff
1998-11-09  0:00             ` Brian Rogoff
1998-11-10  0:00               ` Robert A Duff
1998-11-10  0:00             ` Robert I. Eachus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox