comp.lang.ada
 help / color / mirror / Atom feed
* Dispatching and generics - language lawyer question
@ 2002-07-22 23:13 Adam Beneschan
  2002-07-23 15:42 ` Stephen Leake
  0 siblings, 1 reply; 20+ messages in thread
From: Adam Beneschan @ 2002-07-22 23:13 UTC (permalink / raw)


I have a couple questions about what's supposed to happen when a
generic is instantiated with a class-wide type as an actual type.  I'm
hoping that someone who really understands the intricacies of the
language will be kind enough to answer, and provide chapter and verse
from the RM to explain the correct answer.

(1) What is the correct output of the program below?

(2) What would be the correct output if "private" in line [A] is
changed to
    "tagged private"?

I'm pretty sure the answer to (1) is FALSE, since the "=" on line [B]
in the instance denotes the predefined "=" declared for T1 in the
generic, and not the "=" "defined" for the class-wide type, by
8.3(13).  I'm less clear about the answer to (2).  Here, the "="
implicitly declared for T1 is a dispatching operation, and thus should
dispatch if a controlling operand is of a class-wide type (3.9.2(5)),
but it's not clear to me whether the operands in this case are treated
as if they were of the formal type (which is not class-wide) or of the
actual type (which is class-wide).

Thanks in advance for your help.

				-- Adam




generic
    type T1(<>) is private;            -- [A]
package Pak1 is
    type T1_Access is access T1;
    function Check_Equal (X, Y : T1_Access) return boolean;
end Pak1;    

package body Pak1 is
    function Check_Equal (X, Y : T1_Access) return boolean is
    begin
        return X.all = Y.all;          -- [B]
    end Check_Equal;
end Pak1;    

package Pak2 is
    type Root_Type is tagged record
        F1 : Integer;
    end record;
end Pak2;

with Pak2;
package Pak3 is
    type Child is new Pak2.Root_Type with record
        F2 : Integer;
    end record;
    function "=" (Left, Right : Child) return boolean;
end Pak3;    
        
package body Pak3 is
    function "=" (Left, Right : Child) return boolean is
    begin
        return Left.F1 = Right.F1 and then 
               abs Left.F2 = abs Right.F2;
    end "=";
end Pak3;    
        

with Pak1;
with Pak2;
with Pak3;
with Text_IO;
procedure Test62 is
    package Pak1_Inst is new Pak1 (Pak2.Root_Type'Class);
    A1 : Pak1_Inst.T1_Access;
    A2 : Pak1_Inst.T1_Access;
    B  : Boolean;
begin
    A1 := new Pak3.Child' (F1 => 4, F2 => 6);
    A2 := new Pak3.Child' (F1 => 4, F2 => -6);
    B := Pak1_Inst.Check_Equal (A1, A2);
    Text_IO.Put_Line (Boolean'Image (B));
end Test62;



^ permalink raw reply	[flat|nested] 20+ messages in thread
* Re: Dispatching and generics - language lawyer question
@ 2002-07-24  5:33 Grein, Christoph
  2002-07-24 22:55 ` Robert A Duff
  2002-07-25  0:40 ` Robert Dewar
  0 siblings, 2 replies; 20+ messages in thread
From: Grein, Christoph @ 2002-07-24  5:33 UTC (permalink / raw)


> There is a problem with "reemergence" of predefined operators ...

For untagged types, _redefined_ equality does not correctly compose and 
_predefined_ equality reemerges in generics if not properly transferred by a 
formal parameter:

  type X is something;
  function "=" (L, R: X) return Boolean;  -- redefine

  type Y is record
    C: X;
  end record;

  A, B: Y;
  L: Boolean := A = B;  -- here predefined "=" on component C is used, not the
                        -- equality redefined above

  generic
    type T is private;
    --with function "=" (L, R: T) return Boolean is <>;
  package P is ...

  package P_on_X is new P (X);  -- here also predefined "=" on X is used,
                                -- not the equality redefined above

You have to uncomment the generic parameter function to prevent reemergence.

This whole astonishing rule has been introduced because of compatibility with 
Ada 83 (note there were no tagged types in Ada 83 so there reemergence could 
safely be avoided).

In Ada 83, it was not possible to redefine "=" without using a trick (I do not 
remember the exact way to do it, I never did this; you have to use generics with 
the a limited private formal type). When you used this trick to redefine 
equality, also the predefined equality reemerged as in the cases above. Thus for 
compatibility, we have this rule in Ada 95.



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

end of thread, other threads:[~2002-08-14  0:02 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-22 23:13 Dispatching and generics - language lawyer question Adam Beneschan
2002-07-23 15:42 ` Stephen Leake
2002-07-24 15:37   ` Stephen Leake
  -- strict thread matches above, loose matches on Subject: below --
2002-07-24  5:33 Grein, Christoph
2002-07-24 22:55 ` Robert A Duff
2002-07-25 15:46   ` Ben Brosgol
2002-07-29 20:38     ` Robert A Duff
2002-07-31 22:52       ` Dmitry A.Kazakov
2002-07-31 20:18         ` Robert A Duff
2002-08-02  1:15           ` Dmitry A.Kazakov
2002-08-01 16:30             ` Hyman Rosen
2002-08-02 23:42               ` Dmitry A.Kazakov
2002-08-02 15:49                 ` Hyman Rosen
2002-08-02 17:48                   ` Stephen Leake
2002-08-10  3:03                     ` Warren W. Gay VE3WWG
2002-08-05 11:15                   ` Dmitry A. Kazakov
2002-08-12 12:44                   ` Robert Dewar
2002-08-13 22:50           ` Randy Brukardt
2002-08-14  0:02             ` Robert A Duff
2002-07-25  0:40 ` Robert Dewar

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