comp.lang.ada
 help / color / mirror / Atom feed
* limited private types, "=" operator
@ 1990-08-01 18:47 David Erickson
  1990-08-02  3:34 ` mackey
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: David Erickson @ 1990-08-01 18:47 UTC (permalink / raw)


Is there any way to define "=" for a limited private type which is an
access type (which may be null)?  The problem arises trying to test for
null without producing an infinitely recursive definition.

The only solution that I am aware of is to use UNCHECKED_CONVERSION to
retype the access type during the test for null, but this is a hack.
Is there a cleaner solution?

Thanks -

	Dave Erickson

^ permalink raw reply	[flat|nested] 8+ messages in thread
* limited private types, "=" operator
@ 1990-08-02 13:44 "Norman H. Cohen"
  0 siblings, 0 replies; 8+ messages in thread
From: "Norman H. Cohen" @ 1990-08-02 13:44 UTC (permalink / raw)


Ref: INFO-ADA Digest Volume 90 Issue 148 (Wed, Aug 1, 1990) Item #2

David Erickson asks:

 >Is there any way to define "=" for a limited private type which is an
 >access type (which may be null)?  The problem arises trying to test for
 >null without producing an infinitely recursive definition.

You can define an access type in the private part corresponding to the
internal view of the type as a pointer.  The full type declaration
for the limited private type should derive from this type.  The
redefinition of equality in the package spec will apply only to the
derived type, not to the parent.  Thus two views of the same data
structure are available in the package body, one corresponding to a
pointer view and one corresponding to the abstraction for which "=" has
been redefined.  The two views are embodied by two different types,
each with a version of "=" appropriate to that view.  The package body
can convert between the two types, using not Unchecked_Conversion, but
conversion between a derived type and its parent.  For example:

   package Integer_List_Package is
      type Integer_List_Type is limited private;
      function "=" (Left, Right: Integer_List_Type) return Boolean;
      -- [other subprograms]
   private
      -- Ordinary declaration of a recursive access type:
      type List_Cell_Type;
      type Cell_Pointer_Type is access List_Cell_Type;
      type List_Cell_Type is
         record
            Value_Part : Integer;
            Link_Part  : Cell_Pointer_Type;
         end record;
      -- Full declaration of the limited private type:
      type Integer_List_Type is new Cell_Pointer_Type;
   end Integer_List_Package;

   package body Integer_List_Package is
      function "=" (Left, Right: Integer_List_Type) return Boolean is
         Left_Pointer  : Cell_Pointer_Type := Cell_Pointer_Type (Left);
         Right_Pointer : Cell_Pointer_Type := Cell_Pointer_Type (Right);
      begin
         loop
            if Left_Pointer = null then
               return Right_Pointer = null;
            elsif Right_Pointer = null then
               return False;
            else
               Left_Pointer := Left_Pointer.Link_Part;
               Right_Pointer := Right_Pointer.Link_Part;
         end loop;
      end "=";
      -- [other subprograms]
   end Integer_List_Package;

Left_Pointer and Right_Pointer are declared to be of Cell_Pointer_Type
and initialized using type conversions.  The three = operators in the
if statement all have left operands of Cell_Pointer_Type, so they
invoke the predefined access-type equality for Cell_Pointer_Type, not
(recursively) the redefined "=" for Integer_List_Type.

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

end of thread, other threads:[~1990-08-10 16:20 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1990-08-01 18:47 limited private types, "=" operator David Erickson
1990-08-02  3:34 ` mackey
1990-08-02 17:46 ` David Kassover
1990-08-03 16:01   ` David Erickson
1990-08-04  0:01 ` Charles H. Sampson
1990-08-09 17:19 ` Clay Johnson
1990-08-10 16:20   ` David Erickson
  -- strict thread matches above, loose matches on Subject: below --
1990-08-02 13:44 "Norman H. Cohen"

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