comp.lang.ada
 help / color / mirror / Atom feed
From: Fran�oise & Herv� BITTEUR <hbitteur@club-internet.fr>
Subject: Pb with use of redefined "=" operator
Date: 1998/11/03
Date: 1998-11-03T20:10:14+00:00	[thread overview]
Message-ID: <363F62F3.3FF7@club-internet.fr> (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




             reply	other threads:[~1998-11-03  0:00 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-11-03  0:00 Fran�oise & Herv� BITTEUR [this message]
1998-11-04  0:00 ` Pb with use of redefined "=" operator dewarr
1998-11-04  0:00   ` Tucker Taft
1998-11-04  0:00 ` Mats Weber
1998-11-04  0:00   ` Fran�oise & Herv� BITTEUR
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
replies disabled

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