From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!cs.utexas.edu!sdd.hp.com!decwrl!ucbvax!IBM.COM!NCOHEN From: NCOHEN@IBM.COM ("Norman H. Cohen") Newsgroups: comp.lang.ada Subject: limited private types, "=" operator Message-ID: <9008021357.AA09407@ajpo.sei.cmu.edu> Date: 2 Aug 90 13:44:29 GMT Sender: daemon@ucbvax.BERKELEY.EDU Organization: The Internet List-Id: 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.